:root {
  color-scheme: dark;
  font-family: "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  background: #081116;
  --cream: #fff5d9;
  --lime: #c9f43a;
  --red: #ff4d3f;
  --bg-top: #14252a;
  --bg-bottom: #071116;
  --bg-glow: rgba(104, 149, 104, .28);
  --panel-top: rgba(25,42,44,.94);
  --panel-bottom: rgba(8,19,22,.94);
  --glass: rgba(7, 15, 18, .62);
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #081116;
  touch-action: none;
  user-select: none;
}

body {
  display: grid;
  place-items: center;
}

.game-shell {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  isolation: isolate;
  background:
    radial-gradient(circle at 50% 115%, var(--bg-glow), transparent 42%),
    linear-gradient(160deg, var(--bg-top), var(--bg-bottom) 68%);
  transition: background .45s ease;
}

.game-shell[data-theme="caramel"] {
  --cream: #fff0d0;
  --lime: #ffd064;
  --red: #ff7259;
  --bg-top: #40271c;
  --bg-bottom: #140b08;
  --bg-glow: rgba(222, 142, 59, .32);
  --panel-top: rgba(69,42,29,.95);
  --panel-bottom: rgba(25,13,9,.95);
  --glass: rgba(28, 15, 9, .68);
}

.game-shell[data-theme="berry"] {
  --cream: #fff1f8;
  --lime: #aef46e;
  --red: #ff5b9d;
  --bg-top: #321b38;
  --bg-bottom: #100917;
  --bg-glow: rgba(230, 77, 150, .3);
  --panel-top: rgba(58,29,65,.95);
  --panel-bottom: rgba(20,10,28,.95);
  --glass: rgba(25, 11, 31, .68);
}

.game-shell::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  opacity: .16;
  background-image:
    repeating-linear-gradient(112deg, transparent 0 38px, rgba(255,255,255,.04) 39px 40px),
    repeating-linear-gradient(22deg, transparent 0 70px, rgba(0,0,0,.22) 71px 73px);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}

.hud {
  position: absolute;
  inset: max(20px, env(safe-area-inset-top)) 22px auto;
  display: grid;
  grid-template-columns: auto auto auto 1fr;
  align-items: center;
  gap: 10px;
  pointer-events: none;
}

.score-card, .time-card, .best-card {
  min-width: 84px;
  padding: 9px 14px 10px;
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 13px;
  background: var(--glass);
  backdrop-filter: blur(12px);
  box-shadow: 0 8px 30px rgba(0,0,0,.18);
}

.label {
  display: block;
  color: rgba(255,255,255,.55);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: .18em;
}

.hud strong {
  display: block;
  color: var(--cream);
  font-size: 26px;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.best-card strong { color: var(--lime); font-size: 20px; }
.time-card strong {
  color: #7de8ff;
  font-size: 22px;
  font-variant-numeric: tabular-nums;
  transition: color .2s, transform .2s;
}
.time-card.urgent strong {
  color: var(--red);
  transform: scale(1.06);
  text-shadow: 0 0 16px rgba(255, 77, 63, .55);
}

.lives {
  justify-self: end;
  color: var(--red);
  font-size: 19px;
  letter-spacing: .26em;
  text-shadow: 0 0 18px rgba(255, 77, 63, .48);
}

.room-chip {
  position: absolute;
  top: max(84px, calc(env(safe-area-inset-top) + 64px));
  left: 22px;
  padding: 7px 11px;
  border: 1px solid rgba(255,255,255,.13);
  border-radius: 999px;
  color: rgba(255,255,255,.65);
  background: var(--glass);
  font-size: 10px;
  font-weight: 900;
  letter-spacing: .09em;
  pointer-events: none;
}

.sound-button, .editor-button {
  position: absolute;
  right: 22px;
  bottom: max(22px, env(safe-area-inset-bottom));
  width: 46px;
  height: 46px;
  border: 1px solid rgba(255,255,255,.18);
  border-radius: 50%;
  color: var(--cream);
  background: var(--glass);
  backdrop-filter: blur(12px);
  font: 800 21px/1 inherit;
  cursor: pointer;
  z-index: 4;
}

.editor-button {
  left: 22px;
  right: auto;
  width: auto;
  padding: 0 17px;
  border-radius: 999px;
  font-size: 13px;
  letter-spacing: .08em;
  transition: opacity .2s, transform .2s, background .2s;
}

.editor-button:hover { transform: translateY(-2px); background: color-mix(in srgb, var(--lime) 18%, var(--glass)); }
.editor-button span { color: var(--lime); font-size: 17px; }
.game-shell.playing .editor-button { opacity: 0; pointer-events: none; }

.sound-button.muted { color: rgba(255,255,255,.35); }

.lobby-overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(circle at 50% 35%, rgba(35,61,58,.96), rgba(4,10,12,.98));
}
.lobby-overlay[hidden] { display: none; }
.lobby-panel {
  width: min(430px, 94vw);
  padding: 32px;
  border: 1px solid rgba(255,255,255,.15);
  border-radius: 25px;
  color: var(--cream);
  background: linear-gradient(145deg, var(--panel-top), var(--panel-bottom));
  box-shadow: 0 30px 100px rgba(0,0,0,.58);
}
.lobby-panel h2 { margin: 4px 0 7px; font-size: 30px; }
.lobby-panel>p:not(.editor-kicker):not(.join-status) { margin: 0 0 22px; color: rgba(255,255,255,.55); font-size: 13px; line-height: 1.6; }
.lobby-panel label { display: block; margin: 14px 0 7px; color: rgba(255,255,255,.68); font-size: 11px; font-weight: 900; letter-spacing: .08em; }
.lobby-panel input {
  width: 100%; height: 48px; padding: 0 14px; border: 1px solid rgba(255,255,255,.15); border-radius: 12px;
  outline: 0; color: white; background: rgba(0,0,0,.28); font: 800 16px/1 inherit; text-transform: uppercase;
}
.lobby-panel input:focus { border-color: var(--lime); box-shadow: 0 0 0 3px color-mix(in srgb,var(--lime) 15%,transparent); }
.join-status { min-height: 18px; margin: 10px 0; color: var(--red); font-size: 12px; }
.join-button { width: 100%; height: 48px; border: 0; border-radius: 999px; color: #13200e; background: var(--lime); font: 900 14px/1 inherit; cursor: pointer; }
.join-button:disabled { opacity: .55; cursor: wait; }
.admin-entry { display: block; margin-top: 17px; text-align: center; color: rgba(255,255,255,.48); font-size: 11px; }

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  opacity: 0;
  visibility: hidden;
  transition: opacity .35s ease, visibility .35s;
  background: radial-gradient(circle at center, rgba(5,12,14,.24), rgba(5,12,14,.72));
  backdrop-filter: blur(3px);
  z-index: 3;
}

.overlay.visible { opacity: 1; visibility: visible; }

.panel {
  position: relative;
  width: min(420px, 92vw);
  padding: 42px 36px 32px;
  text-align: center;
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 32px 8px 32px 8px;
  background: linear-gradient(145deg, var(--panel-top), var(--panel-bottom));
  box-shadow: 0 30px 90px rgba(0,0,0,.48), inset 0 1px rgba(255,255,255,.08);
  transform: rotate(-1deg);
}

.panel::before, .panel::after {
  content: "";
  position: absolute;
  width: 70px;
  height: 4px;
  border-radius: 99px;
  background: var(--lime);
  box-shadow: 0 0 20px rgba(201,244,58,.5);
}

.panel::before { left: -20px; top: 68px; transform: rotate(-48deg); }
.panel::after { right: -18px; bottom: 82px; transform: rotate(-48deg); background: var(--red); }

.eyebrow {
  margin: 0 0 12px;
  color: var(--lime);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: .22em;
}

h1 {
  margin: 0;
  color: var(--cream);
  font-size: clamp(58px, 12vw, 92px);
  font-weight: 1000;
  letter-spacing: -.1em;
  line-height: .75;
  text-shadow: 5px 6px 0 rgba(0,0,0,.28);
  transform: skew(-7deg);
}

h1 span { color: var(--red); }

.intro {
  margin: 30px 0 24px;
  color: rgba(255,255,255,.7);
  line-height: 1.75;
  font-size: 14px;
}
.result-status { min-height: 17px; margin: -14px 0 16px; color: var(--lime); font-size: 12px; font-weight: 800; }

.start-button {
  display: inline-flex;
  align-items: center;
  gap: 18px;
  padding: 8px 8px 8px 25px;
  border: 0;
  border-radius: 999px;
  color: #102014;
  background: var(--lime);
  font: 900 16px/1 inherit;
  cursor: pointer;
  box-shadow: 0 9px 30px rgba(201,244,58,.2);
  transition: transform .15s ease, filter .15s ease;
}

.start-button:hover { transform: scale(1.04) rotate(1deg); filter: brightness(1.08); }
.start-button:active { transform: scale(.98); }
.start-button b {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  color: var(--lime);
  background: #102014;
  font-size: 20px;
}

.controls { margin: 20px 0 0; color: rgba(255,255,255,.36); font-size: 11px; letter-spacing: .12em; }

.combo {
  position: absolute;
  left: 50%;
  top: 28%;
  color: var(--cream);
  font-size: clamp(30px, 6vw, 54px);
  font-weight: 1000;
  font-style: italic;
  text-shadow: 3px 4px 0 #e33027, 0 0 25px rgba(255,255,255,.2);
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, 10px) scale(.65) rotate(-5deg);
}

.combo.pop { animation: combo-pop .72s ease-out; }
@keyframes combo-pop {
  0% { opacity: 0; transform: translate(-50%, 16px) scale(.55) rotate(-7deg); }
  24% { opacity: 1; transform: translate(-50%, 0) scale(1.12) rotate(-4deg); }
  72% { opacity: 1; transform: translate(-50%, -8px) scale(1) rotate(-4deg); }
  100% { opacity: 0; transform: translate(-50%, -24px) scale(.95) rotate(-4deg); }
}

.editor-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 22px;
  background: rgba(3, 8, 10, .78);
  backdrop-filter: blur(14px);
  opacity: 0;
  transition: opacity .22s ease;
}

.editor-overlay[hidden] { display: none; }
.editor-overlay.visible { opacity: 1; }

.editor-panel {
  width: min(780px, 96vw);
  max-height: min(88vh, 820px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,.16);
  border-radius: 26px;
  color: var(--cream);
  background: linear-gradient(145deg, var(--panel-top), var(--panel-bottom));
  box-shadow: 0 35px 110px rgba(0,0,0,.6), inset 0 1px rgba(255,255,255,.08);
}

.editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 24px 28px 18px;
  border-bottom: 1px solid rgba(255,255,255,.1);
}

.editor-kicker { margin: 0 0 3px; color: var(--lime); font: 900 10px/1 inherit; letter-spacing: .22em; }
.editor-header h2 { margin: 0; font-size: 25px; letter-spacing: -.04em; }
.close-editor {
  width: 38px;
  height: 38px;
  border: 1px solid rgba(255,255,255,.13);
  border-radius: 50%;
  color: var(--cream);
  background: rgba(255,255,255,.06);
  font: 300 27px/1 inherit;
  cursor: pointer;
}

.editor-scroll { overflow-y: auto; padding: 4px 28px 20px; scrollbar-color: var(--lime) transparent; }
.editor-section { padding: 22px 0 24px; border-bottom: 1px solid rgba(255,255,255,.09); }
.editor-section:last-child { border-bottom: 0; }
.section-heading { display: flex; align-items: center; gap: 12px; margin-bottom: 17px; }
.section-heading > span {
  display: grid;
  place-items: center;
  width: 35px;
  height: 35px;
  flex: 0 0 auto;
  border-radius: 10px;
  color: #15200c;
  background: var(--lime);
  font-size: 11px;
  font-weight: 1000;
}
.section-heading h3 { margin: 0 0 2px; font-size: 16px; }
.section-heading p { margin: 0; color: rgba(255,255,255,.43); font-size: 11px; }
.field-label, .theme-fieldset legend, .difficulty-fieldset legend { display: block; margin: 0 0 8px; color: rgba(255,255,255,.62); font-size: 11px; font-weight: 800; letter-spacing: .1em; }
.text-field, .rules-grid input {
  width: 100%;
  border: 1px solid rgba(255,255,255,.13);
  border-radius: 11px;
  outline: none;
  color: var(--cream);
  background: rgba(0,0,0,.22);
  font: 800 16px/1 inherit;
}
.text-field { height: 46px; padding: 0 14px; margin-bottom: 20px; }
.text-field:focus, .rules-grid input:focus { border-color: var(--lime); box-shadow: 0 0 0 3px color-mix(in srgb, var(--lime) 16%, transparent); }
.theme-fieldset { margin: 0; padding: 0; border: 0; }
.theme-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.theme-option {
  position: relative;
  display: grid;
  grid-template-columns: 38px 1fr;
  grid-template-rows: auto auto;
  gap: 2px 10px;
  align-items: center;
  padding: 12px;
  border: 1px solid rgba(255,255,255,.11);
  border-radius: 14px;
  cursor: pointer;
  background: rgba(255,255,255,.035);
}
.theme-option:has(input:checked) { border-color: var(--lime); box-shadow: inset 0 0 0 1px var(--lime); }
.theme-option input { position: absolute; opacity: 0; }
.theme-swatch { grid-row: 1 / 3; width: 38px; height: 38px; border-radius: 11px; background: linear-gradient(145deg,#233b3d,#071116); box-shadow: inset 0 0 0 1px rgba(255,255,255,.12); }
.theme-caramel .theme-swatch { background: linear-gradient(145deg,#8b5935,#190d08); }
.theme-berry .theme-swatch { background: linear-gradient(145deg,#85416f,#160c20); }
.theme-option b { font-size: 12px; }
.theme-option small { color: rgba(255,255,255,.4); font-size: 9px; }

.difficulty-fieldset { margin: 0; padding: 0; border: 0; }
.difficulty-scale {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
  padding: 6px;
  border: 1px solid rgba(255,255,255,.11);
  border-radius: 16px;
  background: rgba(0,0,0,.18);
}
.difficulty-option { position: relative; cursor: pointer; }
.difficulty-option input { position: absolute; opacity: 0; pointer-events: none; }
.difficulty-option span {
  display: grid;
  place-items: center;
  gap: 4px;
  min-height: 59px;
  border-radius: 11px;
  color: rgba(255,255,255,.58);
  transition: .18s ease;
}
.difficulty-option b { font-size: 17px; }
.difficulty-option small { font-size: 9px; }
.difficulty-option:hover span { background: rgba(255,255,255,.055); }
.difficulty-option:has(input:checked) span {
  color: #15200c;
  background: var(--lime);
  box-shadow: 0 8px 24px color-mix(in srgb, var(--lime) 18%, transparent);
}

.upload-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.upload-box {
  display: grid;
  gap: 5px;
  padding: 19px;
  border: 1px dashed color-mix(in srgb, var(--lime) 55%, transparent);
  border-radius: 15px;
  cursor: pointer;
  background: color-mix(in srgb, var(--lime) 5%, transparent);
}
.upload-box:hover { background: color-mix(in srgb, var(--lime) 10%, transparent); }
.upload-box b { color: var(--lime); font-size: 13px; }
.upload-box span { color: rgba(255,255,255,.58); font-size: 11px; }
.upload-box small { margin-top: 8px; color: rgba(255,255,255,.35); font-size: 10px; }
.upload-box input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.danger-upload { border-color: color-mix(in srgb, var(--red) 55%, transparent); background: color-mix(in srgb, var(--red) 5%, transparent); }
.danger-upload b { color: var(--red); }

.rules-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.rules-grid label { color: rgba(255,255,255,.67); font-size: 10px; font-weight: 800; }
.rules-grid input { height: 45px; margin: 7px 0 5px; padding: 0 10px; font-size: 18px; }
.rules-grid small { display: block; color: rgba(255,255,255,.34); font-size: 9px; font-weight: 500; }
.editor-actions { display: flex; justify-content: flex-end; gap: 10px; padding: 17px 28px 21px; border-top: 1px solid rgba(255,255,255,.1); background: rgba(0,0,0,.12); }
.secondary-button, .save-button { height: 43px; padding: 0 20px; border-radius: 999px; font: 900 12px/1 inherit; cursor: pointer; }
.secondary-button { border: 1px solid rgba(255,255,255,.15); color: var(--cream); background: transparent; }
.save-button { border: 0; color: #15200c; background: var(--lime); }
.save-button:disabled { opacity: .5; cursor: wait; }

@media (max-width: 540px) {
  .hud { inset-inline: 14px; gap: 7px; }
  .score-card, .time-card, .best-card { min-width: 62px; padding: 8px 9px; }
  .hud strong { font-size: 22px; }
  .best-card strong { font-size: 17px; }
  .time-card strong { font-size: 17px; }
  .panel { padding-inline: 24px; }
  .sound-button { right: 14px; }
  .editor-button { left: 14px; }
  .editor-overlay { padding: 8px; }
  .editor-panel { width: 100%; max-height: 95vh; border-radius: 20px; }
  .editor-header { padding: 19px 18px 15px; }
  .editor-scroll { padding-inline: 18px; }
  .theme-grid { grid-template-columns: 1fr; }
  .upload-grid { grid-template-columns: 1fr; }
  .rules-grid { grid-template-columns: 1fr 1fr; }
  .editor-actions { padding: 14px 18px 17px; }
}
