:root {
  --paper: #fffdf6;
  --ink: #2f3542;
  --brand: #6c5ce7;
  --brand-soft: #efeaff;
  --track: #e3ddf7;
  --guide: #c9d4e2;
  --good: #16b26b;
  --bad: #ff3b30;
  --sheet: #ffffff;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: linear-gradient(160deg, #f3f0ff 0%, #e8f4ff 55%, #eafaf1 100%);
  color: var(--ink);
  font-family: "Nunito", "Baloo 2", "SF Pro Rounded", -apple-system, BlinkMacSystemFont,
    "Segoe UI", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body {
  display: flex;
  flex-direction: column;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}

/* ---------------- top bar ---------------- */

#topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
}

.icon-btn {
  flex: 0 0 auto;
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 3px 0 rgba(108, 92, 231, 0.18);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(108, 92, 231, 0.18); }

.title {
  flex: 1 1 auto;
  min-width: 0;
  text-align: center;
}

#letterWord {
  font-size: clamp(15px, 2.4vw, 22px);
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hint {
  font-size: clamp(11px, 1.7vw, 15px);
  font-weight: 700;
  color: var(--brand);
  min-height: 1.2em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hint.bad { color: var(--bad); }
.hint.good { color: var(--good); }

.pips {
  flex: 0 0 auto;
  font-size: 13px;
  letter-spacing: 2px;
  color: var(--brand);
  white-space: nowrap;
}

.marks {
  flex: 0 0 auto;
  font-size: 15px;
  font-weight: 800;
  color: var(--brand);
  white-space: nowrap;
  background: var(--brand-soft);
  border-radius: 12px;
  padding: 5px 9px;
}

.stars {
  flex: 0 0 auto;
  font-size: 20px;
  letter-spacing: 1px;
  min-width: 68px;
  text-align: right;
}

/* ---------------- stage ---------------- */

#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  margin: 0 10px;
  border-radius: 26px;
  background: var(--paper);
  box-shadow: 0 10px 30px rgba(47, 53, 66, 0.12);
  overflow: hidden;
}

#board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  display: block;
}

#stage.shake { animation: shake 0.32s ease; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-9px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(4px); }
}

/* red out-of-line flash */
#flash {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 40;
  opacity: 0;
  box-shadow: inset 0 0 0 14px var(--bad), inset 0 0 90px 30px rgba(255, 59, 48, 0.55);
  background: rgba(255, 59, 48, 0.1);
}
#flash.on { animation: flash 0.42s ease-out; }
@keyframes flash {
  0% { opacity: 0; }
  12% { opacity: 1; }
  100% { opacity: 0; }
}

.celebrate {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
}
.celebrate span {
  font-size: clamp(38px, 9vw, 96px);
  font-weight: 900;
  color: var(--good);
  text-shadow: 0 6px 0 rgba(22, 178, 107, 0.22);
}
.celebrate.on { animation: pop 1.5s ease-out; }
@keyframes pop {
  0% { opacity: 0; transform: scale(0.5); }
  20% { opacity: 1; transform: scale(1.12); }
  70% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.05); }
}

/* ---------------- controls ---------------- */

#controls {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  padding: 10px 12px 12px;
}

.ctl {
  flex: 0 0 auto;
  min-width: 56px;
  height: 56px;
  padding: 0 14px;
  border: none;
  border-radius: 18px;
  background: #fff;
  box-shadow: 0 4px 0 rgba(108, 92, 231, 0.2);
  font-size: 18px;
  font-weight: 800;
  font-family: inherit;
  color: var(--ink);
  cursor: pointer;
}
.ctl.wide { flex: 1 1 auto; }
.ctl:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(108, 92, 231, 0.2); }
.ctl[disabled] { opacity: 0.4; }
.ctl span { font-size: clamp(13px, 2vw, 17px); }

/* ---------------- sheets ---------------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(47, 53, 66, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet[hidden] { display: none; }

.sheet-inner {
  width: min(680px, 100%);
  max-height: 88vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--sheet);
  border-radius: 26px 26px 0 0;
  padding: 16px 18px calc(22px + var(--safe-b));
  animation: rise 0.22s ease-out;
}
@keyframes rise { from { transform: translateY(24px); opacity: 0.4; } }

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.sheet-head h2 { margin: 0; font-size: 20px; }

.close {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 14px;
  background: #f1f0f6;
  font-size: 17px;
  cursor: pointer;
}

.tabs { display: flex; gap: 8px; }
.tab {
  border: none;
  border-radius: 14px;
  padding: 10px 16px;
  font-size: 16px;
  font-weight: 800;
  font-family: inherit;
  background: #f1f0f6;
  color: var(--ink);
  cursor: pointer;
}
.tab.is-on { background: var(--brand); color: #fff; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 10px;
}
.cell {
  position: relative;
  aspect-ratio: 1;
  border: none;
  border-radius: 16px;
  background: var(--brand-soft);
  color: var(--brand);
  font-size: 26px;
  font-weight: 900;
  font-family: inherit;
  cursor: pointer;
}
.cell.is-on { background: var(--brand); color: #fff; }
.cell .cellstars {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 3px;
  font-size: 9px;
  letter-spacing: 1px;
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 4px;
  border-bottom: 1px solid #f0eef7;
  font-size: 16px;
  font-weight: 700;
}
.row input[type="checkbox"] {
  width: 52px;
  height: 32px;
  accent-color: var(--brand);
  transform: scale(1.15);
}

.seg { display: flex; gap: 6px; }
.seg button {
  border: none;
  border-radius: 12px;
  padding: 9px 12px;
  background: #f1f0f6;
  font-size: 14px;
  font-weight: 800;
  font-family: inherit;
  color: var(--ink);
  cursor: pointer;
}
.seg button.is-on { background: var(--brand); color: #fff; }

.note { font-size: 13px; color: #7a8290; line-height: 1.45; }
.danger {
  margin-top: 12px;
  width: 100%;
  border: none;
  border-radius: 14px;
  padding: 14px;
  background: #ffecec;
  color: #d63031;
  font-size: 15px;
  font-weight: 800;
  font-family: inherit;
  cursor: pointer;
}

/* short/landscape phones: tighten things up */
@media (max-height: 480px) {
  .icon-btn { width: 40px; height: 40px; font-size: 18px; }
  .ctl { height: 46px; min-width: 46px; }
  #topbar { padding: 4px 10px; }
  #controls { padding: 6px 10px 8px; }
}

/* ---------------- dark theme ---------------- */

body.dark {
  --paper: #1f232c;
  --ink: #e9edf4;
  --brand: #a79bff;
  --brand-soft: #2e2a4a;
  --sheet: #232833;
  background: linear-gradient(160deg, #171a21 0%, #191d28 55%, #14201c 100%);
}
body.dark .icon-btn,
body.dark .ctl {
  background: #2b3140;
  color: var(--ink);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.45);
}
body.dark .icon-btn { box-shadow: 0 3px 0 rgba(0, 0, 0, 0.45); }
body.dark .ctl:active { box-shadow: 0 1px 0 rgba(0, 0, 0, 0.45); }
body.dark .sheet { background: rgba(0, 0, 0, 0.62); }
body.dark .tab,
body.dark .seg button,
body.dark .close { background: #2b3140; color: var(--ink); }
body.dark .tab.is-on,
body.dark .seg button.is-on,
body.dark .cell.is-on { background: var(--brand); color: #14171d; }
body.dark .note { color: #97a1b3; }
body.dark .danger { background: #3a2226; color: #ff8b84; }
