/* Guess tiles and the answer: Kantumruy Pro */
@font-face { font-family: "Kantumruy Pro"; font-weight: 400; font-display: swap; src: url(fonts/kantumruy-pro-khmer-400-normal.woff2) format("woff2"); unicode-range: U+1780-17FF, U+19E0-19FF, U+200C-200D, U+25CC; }
@font-face { font-family: "Kantumruy Pro"; font-weight: 600; font-display: swap; src: url(fonts/kantumruy-pro-khmer-600-normal.woff2) format("woff2"); unicode-range: U+1780-17FF, U+19E0-19FF, U+200C-200D, U+25CC; }
@font-face { font-family: "Kantumruy Pro"; font-weight: 400; font-display: swap; src: url(fonts/kantumruy-pro-latin-400-normal.woff2) format("woff2"); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+20AC, U+2122, U+2212, U+FEFF, U+FFFD; }

/* Everything else: pixel fonts. Peak Pixel is Kantumruy Pro redrawn on a 16 px grid
   (tools/pixelate_font.py), so Khmer still stacks correctly; Latin is Pixelify Sans. */
@font-face { font-family: "Pixel UI"; font-display: swap; src: url(fonts/peak-pixel-khmer.woff2) format("woff2"); unicode-range: U+1780-17FF, U+19E0-19FF, U+200C-200D, U+25CC; }
@font-face { font-family: "Pixel UI"; font-display: swap; src: url(fonts/pixelify-sans-latin-400-normal.woff2) format("woff2"); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+20AC, U+2122, U+2212, U+FEFF, U+FFFD; }
@font-face { font-family: "Peak Pixel Display"; font-display: swap; src: url(fonts/peak-pixel-display.woff2) format("woff2"); }

:root {
  --mist: #E9EEF2;
  --ink: #17233A;
  --ink-soft: #55627A;
  --paper: #FFFFFF;
  --line: #B9C3CF;
  --lotus: #B8325A;
  --correct: #2E7454;
  --present: #D98A1F;
  --absent: #6E7685;
  --key: #D3DBE4;
  --keycap: #FFFFFF;
  --keyspecial: #AEB8C5;
  --on-state: #FFFFFF;
  --t: 52px;
  --key-h: clamp(38px, 6.2dvh, 52px);
  --pad: clamp(8px, 3vw, 16px);
  --word-font: "Kantumruy Pro", "Noto Sans Khmer", "Khmer OS", system-ui, sans-serif;
  font-family: "Pixel UI", "Kantumruy Pro", "Noto Sans Khmer", system-ui, sans-serif;
  font-synthesis: none;
  color: var(--ink);
  background: var(--mist);
  -webkit-text-size-adjust: 100%;
}
/* Dark theme: follows the device unless the player picked one with the theme button (data-theme on <html>) */
:root { color-scheme: light; }
:root[data-theme="dark"] {
    --mist: #111A29; --ink: #E6EBF2; --ink-soft: #9AA6BA; --paper: #1B2537;
    --line: #3A475D; --key: #1F2A3C; --keycap: #4A5668; --keyspecial: #2E3A4E; --absent: #3E4859; --correct: #2F8A60; --present: #C98426;
    --lotus: #E0668B; color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --mist: #111A29; --ink: #E6EBF2; --ink-soft: #9AA6BA; --paper: #1B2537;
    --line: #3A475D; --key: #1F2A3C; --keycap: #4A5668; --keyspecial: #2E3A4E; --absent: #3E4859; --correct: #2F8A60; --present: #C98426;
    --lotus: #E0668B; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body { background: var(--mist); touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, .tile:focus-visible { outline: 3px solid var(--lotus); outline-offset: 2px; }

/* ---------- shell ---------- */
.app {
  position: relative; overflow: hidden;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-areas: "top" "mode" "boards" "panel";
  height: 100vh; height: 100dvh;
  max-width: 600px; margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) max(var(--pad), env(safe-area-inset-right)) 0 max(var(--pad), env(safe-area-inset-left));
}
.top { grid-area: top; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.mode-line { grid-area: mode; margin: 0; font-size: 14px; color: var(--ink-soft); }
.boards { grid-area: boards; }
#panel, #end { grid-area: panel; }

.title { margin: 0; display: flex; align-items: baseline; gap: 10px; font-weight: 400; min-width: 0; }
.km-title { font-family: "Peak Pixel Display", "Pixel UI", serif; font-size: clamp(24px, 7vw, 32px); line-height: 1.5; color: var(--lotus); }
.en-title { font-size: 15px; font-weight: 500; color: var(--ink-soft); white-space: nowrap; }
.top-actions { display: flex; gap: 6px; flex: none; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%; border: 1.5px solid var(--line); padding: 0;
  background: transparent; font-size: 18px; font-weight: 600; display: grid; place-items: center;
}
.icon-btn.lang-btn { width: auto; min-width: 38px; padding: 0 10px; border-radius: 19px; font-size: 15px; line-height: 1; }
#btn-theme .i-sun { display: none; }
#btn-theme.is-dark .i-sun { display: block; }
#btn-theme.is-dark .i-moon { display: none; }

/* Khmer needs taller lines for stacked vowels and subscripts */
html:lang(km) .dlg { font-size: 17px; line-height: 1.9; }
html:lang(km) .mode-line, html:lang(km) .end-result, html:lang(km) .end-next { line-height: 1.8; }
html:lang(km) .mode-line, html:lang(km) .end-next, html:lang(km) .dist-row { font-size: 15px; }
html:lang(km) .stats span { font-size: 14px; }

/* sits over the bottom of the boards, just above the keyboard, without moving anything */
.toast {
  grid-area: boards; align-self: end; justify-self: center; position: relative; z-index: 5;
  margin-bottom: 8px; transform: translateY(4px); opacity: 0; pointer-events: none;
  padding: 4px 12px; border-radius: 7px; background: var(--ink); color: var(--mist);
  font-size: 15px; line-height: 1.5; text-align: center; max-width: min(92%, 440px);
  transition: opacity .15s, transform .15s;
}
.toast.word { font-family: var(--word-font); font-size: 17px; }
.toast.show { opacity: 1; transform: translateY(0); }

/* ---------- boards ---------- */
.boards {
  min-height: 0; overflow: hidden;
  display: grid; grid-template-columns: repeat(var(--bcols, 1), auto);
  justify-content: center; align-content: safe center;
  row-gap: var(--bgap, 10px); column-gap: var(--bcgap, 40px); padding: 6px 0; scrollbar-width: thin;
}
.board {
  display: grid; gap: calc(var(--t) * .09); flex: none; align-self: start;
  grid-template-columns: repeat(var(--cols), var(--t));
  grid-template-rows: var(--t) calc(var(--t) * .62);
}
.tile {
  font-family: var(--word-font);
  display: grid; place-items: center; border-radius: calc(var(--t) * .14);
  border: 2px solid var(--line); background: var(--paper);
  font-size: calc(var(--t) * .46); line-height: 1; user-select: none; -webkit-user-select: none;
}
.tile.sub { border-radius: calc(var(--t) * .08) calc(var(--t) * .08) calc(var(--t) * .24) calc(var(--t) * .24); font-size: calc(var(--t) * .36); border-style: dashed; }
.tile.filled { border-color: var(--ink-soft); border-style: solid; }
.board.current .tile { cursor: pointer; }
.board.current .tile.sel { border-color: var(--lotus); box-shadow: inset 0 calc(var(--t) * -.07) 0 var(--lotus); }
.board.future .tile { opacity: .45; }
.tile.s-correct, .tile.s-present, .tile.s-absent { border-color: transparent; border-style: solid; color: var(--on-state); }
.s-correct { background: var(--correct) !important; }
.s-present { background: var(--present) !important; }
.s-absent { background: var(--absent) !important; }
.board.shake { animation: shake .35s; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
@media (prefers-reduced-motion: no-preference) {
  .tile.flip { animation: flip .5s ease both; }
  @keyframes flip { 0% { transform: rotateX(0); } 50% { transform: rotateX(90deg); } 100% { transform: rotateX(0); } }
}

/* ---------- keyboard tray ---------- */
.panel {
  container-type: inline-size;
  margin: 0 calc(-1 * max(var(--pad), env(safe-area-inset-right))) 0 calc(-1 * max(var(--pad), env(safe-area-inset-left)));
  padding: 7px 3px max(6px, env(safe-area-inset-bottom));
  background: var(--key);
}
.keys { display: flex; flex-direction: column; gap: clamp(6px, 1.3dvh, 11px); --gap: clamp(3px, 1.2cqi, 7px); }
.key-row { display: flex; gap: var(--gap); justify-content: center; }
.key {
  font-family: var(--word-font);  /* keys use the same readable font as the boxes, not the pixel font */
  position: relative;
  flex: 0 0 calc((100% - 10 * var(--gap)) / 11); height: var(--key-h); padding: 0; border: 0; border-radius: 6px;
  background: var(--keycap); box-shadow: 0 1px 0 rgba(0, 0, 0, .25);
  font-size: clamp(15px, 5.2cqi, 25px); line-height: 1;
  display: grid; place-items: center; touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
.key:active:not(:disabled) { filter: brightness(.88); }
.key:disabled, .key.k-off { opacity: .25; cursor: default; box-shadow: none; }
.key.k-off:active { filter: none; }
.key.k-wide { flex: 1 1 0; background: var(--keyspecial); font-size: clamp(16px, 5cqi, 22px); }
.key.k-on { background: var(--ink); color: var(--mist); }
.key.k-sub { box-shadow: inset 0 -3px 0 var(--ink-soft); }
.key.k-coeng.k-on:disabled { opacity: 1; }
.key.k-layer { flex: 0 0 25%; background: var(--keyspecial); font-size: clamp(13px, 3.8cqi, 17px); }
.key.k-enter { flex: 1 1 0; background: var(--ink); color: var(--mist); font-size: clamp(14px, 4cqi, 17px); font-weight: 500; }
.key.s-correct, .key.s-present, .key.s-absent { color: var(--on-state); }

/* iPhone-style key preview while a finger is on a letter */
@media (hover: none) and (pointer: coarse) {
  .key[data-l]:active:not(:disabled):not(.k-off)::after {
    content: attr(data-l); position: absolute; z-index: 3; left: 50%; bottom: calc(100% + 4px);
    transform: translateX(-50%); min-width: 150%; padding: 6px 4px;
    border-radius: 8px; background: inherit; color: inherit; box-shadow: 0 2px 6px rgba(0, 0, 0, .3);
    font-size: 1.6em; line-height: 1.2;
  }
  .key[data-l]:active:not(:disabled):not(.k-off) { filter: none; }
}

/* ---------- end of game ---------- */
.end {
  text-align: center; padding: 10px 0 max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}
.panel.end { background: transparent; margin: 0; }
.end-result { margin: 0; font-size: 16px; color: var(--ink-soft); }
.end-word { font-family: var(--word-font); margin: 0; font-size: clamp(32px, 10vw, 44px); font-weight: 600; line-height: 1.6; }
.end-actions { display: flex; gap: 8px; margin-top: 6px; }
.act {
  flex: 1; min-height: 48px; padding: 4px 14px; border-radius: 8px; border: 1.5px solid var(--line);
  background: transparent; font-size: 16px; font-weight: 500; display: grid; place-items: center; line-height: 1.25;
}
.act-main { background: var(--ink); color: var(--mist); border-color: var(--ink); }
.end-next { margin: 8px 0 0; font-size: 14px; color: var(--ink-soft); }
.more-link { display: inline-block; margin-top: 6px; padding: 6px 4px; font-size: 14px; color: var(--lotus); text-underline-offset: 3px; }

/* ---------- dialogs ---------- */
dialog {
  border: 0; border-radius: 14px; padding: 0;
  width: min(440px, calc(100vw - 24px)); max-height: min(92dvh, 760px); overflow: auto;
  background: var(--paper); color: var(--ink);
}
dialog::backdrop { background: rgba(10, 16, 28, .55); }
.dlg { position: relative; padding: 20px 22px 22px; font-size: 16px; line-height: 1.6; }
.dlg h2 { margin: 0 0 8px; font-size: 21px; font-weight: 600; }
.dlg h3 { margin: 18px 0 8px; font-size: 16px; font-weight: 600; }
.dlg p { margin: 0 0 12px; }
.dlg .muted { color: var(--ink-soft); font-size: 14px; margin: 0; }
.dlg-close { position: absolute; top: 8px; right: 10px; border: 0; background: none; font-size: 28px; line-height: 1; padding: 4px 8px; }
.example { display: flex; justify-content: center; margin: 4px 0 12px; }
.board.ex { --t: 46px; }
.legend { list-style: none; padding: 0; margin: 0 0 12px; display: grid; gap: 6px; }
.legend li { display: flex; align-items: center; gap: 10px; }
.sw { width: 18px; height: 18px; border-radius: 4px; flex: none; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; text-align: center; }
.stats b { display: block; font-size: 28px; font-weight: 600; line-height: 1.3; }
.stats span { font-size: 13px; color: var(--ink-soft); line-height: 1.3; display: block; }
.dist { display: grid; gap: 5px; }
.dist-row { display: grid; grid-template-columns: 16px 1fr; gap: 8px; align-items: center; font-size: 14px; }
.dist-bar { background: var(--absent); color: var(--on-state); border-radius: 4px; padding: 1px 8px; text-align: right; min-width: 26px; font-size: 13px; }
.dist-bar.today { background: var(--correct); }

/* ---------- small phones (iPhone SE, mini, short browser windows) ---------- */
@media (max-height: 700px) and (orientation: portrait) {
  .km-title { font-size: 24px; }
  .icon-btn { width: 34px; height: 34px; font-size: 16px; }
  .icon-btn.lang-btn { height: 34px; padding: 0 8px; }
  .mode-line { font-size: 13px; }
  :root { --key-h: clamp(34px, 6.4dvh, 44px); }
}
@media (max-width: 440px) {
  .en-title { display: none; }
}

/* ---------- tablets and desktop ---------- */
@media (min-width: 640px) and (min-height: 700px) {
  .app { padding-bottom: 16px; }
  :root { --key-h: clamp(44px, 5.8dvh, 58px); }
  .panel { margin: 4px 0 0; padding: 10px; border-radius: 16px; }
  .key { border-radius: 8px; }
  .end { border-top: 0; }
}

/* ---------- phones in landscape: boards left, keyboard right ---------- */
@media (orientation: landscape) and (max-height: 540px) {
  .app {
    max-width: none;
    grid-template-columns: minmax(0, 1fr) minmax(320px, 50%);
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas: "top panel" "mode panel" "boards panel";
    column-gap: 14px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }
  .km-title { font-size: 22px; line-height: 1.4; }
  .icon-btn { width: 32px; height: 32px; font-size: 15px; }
  .icon-btn.lang-btn { height: 32px; padding: 0 8px; }
  .mode-line { font-size: 12px; }
  :root { --key-h: clamp(30px, 12dvh, 46px); }
  .keys { gap: clamp(5px, 2dvh, 9px); }
  .panel, .end { margin: 0; align-self: end; border-radius: 14px; padding: 8px 6px; }
  .end { align-self: center; border-top: 0; }
}
