/*
 * വാക്ക് · Vaakku
 * Palette: lime-washed wall, backwater ink, banana-leaf green, kasavu brass,
 * laterite red for focus. Type: Baloo Chettan 2 for letters on the board,
 * Anek Malayalam for everything you tap and read.
 */
:root {
  --bg: #F3F5EF;
  --surface: #FFFFFF;
  --ink: #16271F;
  --ink-soft: #56675D;
  --line: #CAD3CB;
  --line-strong: #8C9B91;
  --key: #DEE5DC;
  --key-vowel: #EAE3CC;
  --key-chillu: #D8E2E5;
  --key-action: #C8D2C7;
  --hit: #257A4B;
  --near: #B8820F;
  --family: #2F6DB5;
  --miss: #6E7A73;
  --on-state: #FFFFFF;
  --accent: #A3462B;
  --gold: #C29B2A;
  --overlay: rgba(14, 26, 20, 0.5);
  --shadow: 0 18px 50px rgba(14, 26, 20, 0.25);
  --font-ui: "Anek Malayalam", "Noto Sans Malayalam", "Manjari", "Kartika", system-ui, sans-serif;
  --font-display: "Baloo Chettan 2", "Anek Malayalam", "Noto Sans Malayalam", "Kartika", system-ui, sans-serif;
  color-scheme: light;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0F1713;
    --surface: #17221C;
    --ink: #E3EBE5;
    --ink-soft: #98A99F;
    --line: #2E3C34;
    --line-strong: #5E7065;
    --key: #28352E;
    --key-vowel: #3A3522;
    --key-chillu: #25333A;
    --key-action: #34443B;
    --hit: #2C8A55;
    --near: #B8820F;
    --family: #3574C2;
    --miss: #3E4943;
    --accent: #E38561;
    --gold: #C9A43A;
    --overlay: rgba(0, 0, 0, 0.6);
    --shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0F1713;
  --surface: #17221C;
  --ink: #E3EBE5;
  --ink-soft: #98A99F;
  --line: #2E3C34;
  --line-strong: #5E7065;
  --key: #28352E;
  --key-vowel: #3A3522;
  --key-chillu: #25333A;
  --key-action: #34443B;
  --hit: #2C8A55;
  --near: #B8820F;
  --family: #3574C2;
  --miss: #3E4943;
  --accent: #E38561;
  --gold: #C9A43A;
  --overlay: rgba(0, 0, 0, 0.6);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.app {
  height: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding-inline: 16px;
  display: flex;
  flex-direction: column;
}

/* ----- Header, edged like a kasavu border */
.masthead {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 10px;
  padding-block: 10px 8px;
  border-bottom: 5px double var(--gold);
}
.switches { flex-basis: 100%; display: flex; justify-content: space-between; gap: 8px; }
.modes .count {
  display: inline-block;
  min-width: 1.4em;
  margin-left: 2px;
  padding: 1px 4px 0;
  border-radius: 999px;
  background: var(--key);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}
.brand { display: flex; align-items: baseline; gap: 6px; margin-right: auto; min-width: 0; }
.name {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 27px;
  line-height: 1.1;
}
.edition { font-size: 13px; font-weight: 600; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.modes { display: inline-flex; padding: 3px; border-radius: 999px; background: var(--key); }
.modes button {
  border: 0;
  padding: 5px 11px 3px;
  border-radius: 999px;
  background: transparent;
  color: var(--ink-soft);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.modes button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.14); }
.tools { display: flex; gap: 4px; }
.icon {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border: 1.5px solid var(--line);
  border-radius: 50%;
  background: transparent;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}

/* ----- Board */
.play {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-block: 10px 12px;
}
.board-wrap {
  flex: 1 1 0;
  min-height: 180px;
  display: flex;
  align-items: center;
  justify-content: center;
  container-type: size;
}
.board {
  --rows: 8;
  width: min(100cqw, calc(100cqh * 4 / (var(--rows) * 0.95)), 280px);
  aspect-ratio: 4 / calc(var(--rows) * 0.95);
  display: grid;
  grid-template-rows: repeat(var(--rows), 1fr);
  gap: 5px;
}
.row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; }
.tile {
  display: grid;
  place-items: center;
  min-width: 0;
  border: 2px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 22px;
  font-size: clamp(13px, min(4.6cqh, 8.5cqw), 28px);
  line-height: 1;
  padding-top: 0.12em;
  user-select: none;
}
.tile.filled:not([data-state]) { border-color: var(--line-strong); animation: pop 120ms ease-out; }
.row.typing .tile { cursor: pointer; }
.tile.active { border-color: var(--accent); box-shadow: inset 0 -4px 0 var(--accent); }
.tile.active.replace { animation: blink 1s ease-in-out infinite; }
@keyframes blink { 50% { border-color: var(--line-strong); box-shadow: inset 0 -4px 0 transparent; } }
.tile[data-state] { color: var(--on-state); border-color: transparent; }
.tile[data-state="hit"] { background: var(--hit); }
.tile[data-state="near"] { background: var(--near); }
.tile[data-state="family"] { background: var(--family); }
.tile[data-state="familyNear"] { background: linear-gradient(135deg, var(--family) 0 50%, var(--near) 50% 100%); }
.tile[data-state="miss"] { background: var(--miss); }
.tile.flip {
  animation: flip 560ms ease-in-out both;
  animation-delay: calc(var(--i) * 280ms);
  transition: background-color 0s, background-image 0s, border-color 0s, color 0s;
  transition-behavior: allow-discrete;
  transition-delay: calc(var(--i) * 280ms + 280ms);
}
.row.shake { animation: shake 420ms; }
.row.win .tile { animation: bounce 760ms ease both; animation-delay: calc(var(--i) * 90ms); }

@keyframes pop { 50% { transform: scale(1.08); } }
@keyframes flip { 50% { transform: rotateX(90deg); } }
@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(3px); }
  30%, 50%, 70% { transform: translateX(-5px); }
  40%, 60% { transform: translateX(5px); }
}
@keyframes bounce {
  0%, 20% { transform: translateY(0); }
  40% { transform: translateY(-14px); }
  50% { transform: translateY(2px); }
  60% { transform: translateY(-5px); }
  80%, 100% { transform: translateY(0); }
}

.toast {
  position: absolute;
  top: 12px;
  left: 50%;
  z-index: 5;
  max-width: calc(100% - 16px);
  padding: 9px 14px 7px;
  border-radius: 8px;
  background: var(--ink);
  color: var(--bg);
  font-weight: 600;
  font-size: 15px;
  text-align: center;
  opacity: 0;
  transform: translate(-50%, -6px);
  transition: opacity 160ms, transform 160ms;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ----- Letters under the current tile: its conjuncts, then its vowel forms */
.forms {
  display: grid;
  gap: 5px;
  min-height: 77px;
  align-content: start;
}
.strip {
  display: flex;
  gap: 5px;
  overflow-x: auto;
  scrollbar-width: none;
  overscroll-behavior-x: contain;
  -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 18px), transparent);
  mask-image: linear-gradient(90deg, #000 calc(100% - 18px), transparent);
  padding-right: 18px;
}
.strip::-webkit-scrollbar { display: none; }
.form {
  flex: 0 0 auto;
  min-width: 40px;
  height: 36px;
  padding: 3px 8px 0;
  border: 1.5px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  font-size: 18px;
  font-weight: 500;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
}
.strip.family .form { background: var(--key-vowel); border-color: transparent; font-weight: 600; }
.form[data-size="m"] { font-size: 16px; }
.form[data-size="l"] { font-size: 14px; }
.strip .form[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.forms-hint {
  grid-column: 1 / -1;
  min-height: 77px;
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: 1.5px dashed var(--line);
  border-radius: 8px;
  color: var(--ink-soft);
  font-size: 15px;
  text-align: center;
  line-height: 1.3;
}
.forms-hint .en { font-size: 13px; }
.forms-hint button {
  margin-top: 4px;
  padding: 5px 14px 3px;
  border: 0;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

/* ----- Aksharamaala: one row per varga pair, halves split like a two-page chart */
.keys { display: flex; flex-direction: column; gap: 6px; }
.key-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.key-half { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; }
.key {
  height: 42px;
  height: clamp(36px, 6dvh, 50px);
  min-width: 0;
  padding: 3px 0 0;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 6px;
  background: var(--key);
  color: var(--ink);
  font-size: 19px;
  font-weight: 500;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}
.key.vowel { background: var(--key-vowel); }
.key.chillu { background: var(--key-chillu); }
.key.wide { grid-column: span 2; }
.key.action { background: var(--key-action); font-size: 16px; font-weight: 700; }
.key.enter { background: var(--ink); color: var(--bg); }
.key.sign { font-size: 24px; background: var(--key-action); }
.key[data-state] { color: var(--on-state); }
.key[data-state="hit"] { background: var(--hit); }
.key[data-state="near"] { background: var(--near); }
.key[data-state="family"] { background: var(--family); }
.key[data-state="familyNear"] { background: linear-gradient(135deg, var(--family) 0 50%, var(--near) 50% 100%); }
.key[data-state="miss"] { background: var(--miss); }
.key:active, .form:active { transform: translateY(1px); filter: brightness(0.94); }

/* ----- Overlays */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 16px;
  overflow-y: auto;
  background: var(--overlay);
}
.sheet:focus { outline: none; }
.sheet {
  position: relative;
  width: min(100%, 440px);
  max-height: 100%;
  overflow-y: auto;
  padding: 22px 20px 20px;
  border-top: 5px double var(--gold);
  border-radius: 14px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
}
.close {
  position: absolute;
  top: 8px; right: 8px;
  width: 36px; height: 36px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
}
.sheet h2 {
  margin: 0 0 12px;
  font-family: var(--font-display);
  font-size: 22px;
  line-height: 1.2;
  text-wrap: balance;
}
.sheet h3 { margin: 18px 0 8px; font-size: 16px; }
.en {
  display: block;
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-soft);
  letter-spacing: 0.01em;
}
.rules { margin: 0; padding-left: 18px; display: grid; gap: 10px; font-size: 15px; line-height: 1.45; }
.example { display: grid; grid-template-columns: repeat(4, 48px); gap: 5px; margin-top: 10px; }
.example .tile { height: 48px; font-size: 19px; animation: none; }
.example-note { margin: 6px 0 0; font-size: 14px; line-height: 1.4; }
.recipe { display: flex; align-items: center; gap: 8px; margin: 4px 0 8px; color: var(--ink-soft); }
kbd {
  min-width: 38px;
  padding: 7px 8px 4px;
  border-radius: 6px;
  background: var(--key);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 18px;
  text-align: center;
}
kbd.made { background: var(--hit); color: var(--on-state); font-family: var(--font-display); font-weight: 700; }
.fine { margin: 10px 0 0; font-size: 14px; line-height: 1.45; }

.result { margin-bottom: 18px; text-align: center; }
.verdict { margin: 4px 0 0; font-family: var(--font-display); font-size: 30px; font-weight: 800; color: var(--hit); }
.verdict.lost { color: var(--accent); }
.verdict-en { margin: 0; color: var(--ink-soft); font-size: 14px; }
.answer-card {
  display: inline-flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 12px;
  padding: 10px 18px 8px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
}
.answer-word { font-family: var(--font-display); font-size: 28px; font-weight: 700; line-height: 1.2; }
.answer-meaning { color: var(--ink-soft); font-size: 14px; }

.numbers { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 0; }
.numbers div { display: flex; flex-direction: column-reverse; align-items: center; text-align: center; }
.numbers dd { margin: 0; font-family: var(--font-display); font-size: 30px; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.numbers dt { font-size: 12px; line-height: 1.25; }
.dist { list-style: none; margin: 0; padding: 0; display: grid; gap: 5px; }
.dist li { display: grid; grid-template-columns: 14px 1fr; align-items: center; gap: 8px; font-variant-numeric: tabular-nums; }
.dist .try { font-size: 14px; font-weight: 600; text-align: right; }
.dist .bar {
  display: block;
  width: var(--w);
  min-width: 26px;
  padding: 3px 8px 1px;
  border-radius: 4px;
  background: var(--miss);
  color: var(--on-state);
  font-size: 14px;
  font-weight: 700;
  text-align: right;
}
.dist li.mine .bar { background: var(--hit); }
.next {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 18px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  font-size: 15px;
}
.next strong { font-family: var(--font-display); font-size: 24px; font-variant-numeric: tabular-nums; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }
.btn {
  min-height: 46px;
  padding: 6px 10px 3px;
  border: 0;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}
.btn.primary { background: var(--hit); color: var(--on-state); }
.btn.ghost { background: var(--key); color: var(--ink); }
.share-fallback {
  width: 100%;
  margin-top: 12px;
  padding: 8px;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.4 var(--font-ui);
  resize: none;
}

@media (max-width: 380px) {
  .masthead { gap: 6px; }
  .name { font-size: 24px; }
  .modes button { padding-inline: 8px; font-size: 13px; }
  .key { font-size: 17px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
