/* Playground styles: a phone-like page with the keyboard at the bottom. */

/* The key font: Kantumruy Pro with an empty dotted circle (scripts/build-key-font.py),
   so a vowel sign on its own shows without one. Coeng's key and the 123 page keep the
   page font. */
@font-face {
  font-family: "Khmer Flick Keys";
  src: url("fonts/KhmerFlickKeys.woff2") format("woff2");
  font-weight: 400 700;
  font-display: block;
}

:root {
  color-scheme: light;
  --ink: #111318;
  --muted: #70747c;
  --accent: #1677ff;
  --keyboard: #d4d7dc;
  --key: #fbfbfc;
  --key-special: #aeb3bb;
  --key-shadow: #858a92;
  --pressed: #c5c9cf;
  --gap: 3px;
  --page: #f6f7f9;
  --surface: #fff;
  --composer-end: #fbfbfc;
  --border: #eceef1;
  /* The key theme (body.theme-lagoon, the default, or body.theme-angkor): the
     vowel keys' tint, the coeng key and its mark, and the next-key hint. */
  --theme-vowel: #d9eeec;
  --theme-coeng: #1c8a82;
  --theme-coeng-ink: #fff;
  --theme-hint: #1c8a82;
}
body.theme-angkor { --theme-vowel: #efe5d3; --theme-coeng: #c0613a; --theme-hint: #c0613a; }
body.night { --theme-vowel: #26403e; --theme-coeng: #2fa99f; --theme-hint: #3cc2b6; }
body.night.theme-angkor { --theme-vowel: #3d362c; --theme-coeng: #d9774f; --theme-hint: #e58a63; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: "Kantumruy Pro", "Noto Sans Khmer", system-ui, sans-serif;
  color: var(--ink);
  background: var(--page);
}

.app-bar,
.keyboard-shell,
#popup {
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

body { display: flex; justify-content: center; }

.phone {
  width: 100%;
  max-width: 520px;
  min-width: 0;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  box-shadow: 0 0 36px rgb(18 25 38 / 8%);
}

.app-bar {
  min-height: 54px;
  padding: max(10px, env(safe-area-inset-top)) 16px 9px;
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  align-items: center;
  gap: 6px;
  border-bottom: 1px solid var(--border);
  font-family: system-ui, sans-serif;
}

.app-bar button {
  appearance: none;
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font: 500 16px/28px system-ui, sans-serif;
}

.app-bar .back { text-align: left; font-size: 25px; line-height: 28px; }
.app-title { min-width: 0; text-align: center; }
.app-title strong { display: block; font: 600 16px/20px "Kantumruy Pro", system-ui, sans-serif; }
.header-actions { display: flex; align-items: center; justify-self: end; gap: 5px; }
.app-bar .done {
  min-width: 48px;
  font-family: "Kantumruy Pro", system-ui, sans-serif;
  text-align: right;
}

.app-bar .display-toggle {
  width: 32px;
  height: 32px;
  min-height: 32px;
  padding: 0;
  border: 1px solid #c9cdd3;
  border-radius: 9px;
  color: var(--muted);
  font: 600 17px/30px system-ui, sans-serif;
  text-align: center;
}

.app-bar .display-toggle[aria-checked="true"] {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.app-bar .display-toggle:disabled { opacity: .42; }
.app-bar .display-toggle[hidden] { display: none; }

.composer {
  flex: 1 1 auto;
  min-height: 0;
  padding: 20px 18px;
  overflow-y: auto;
  background: linear-gradient(var(--surface), var(--composer-end));
  touch-action: auto;
  user-select: text;
  -webkit-user-select: text;
  -webkit-touch-callout: default;
}

.display-toggle {
  appearance: none;
  min-height: 32px;
  padding: 4px 11px;
  border: 1px solid #c9cdd3;
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font: 600 13px/20px system-ui, sans-serif;
}

.display-toggle[aria-checked="true"] {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

#output {
  min-height: 92px;
  padding-top: 16px;
  font-size: clamp(23px, 6.3vw, 30px);
  line-height: 1.65;
  word-break: break-word;
  white-space: pre-wrap;
  cursor: text;
  touch-action: auto;
  user-select: text;
  -webkit-user-select: text;
  caret-color: transparent;
  outline: none;
}

#caret {
  display: inline-block;
  width: 2px;
  height: 1.25em;
  margin-left: 2px;
  vertical-align: -.24em;
  border-radius: 1px;
  background: var(--accent);
  animation: blink 1.05s step-end infinite;
}

#caret[hidden] { display: none; }

@keyframes blink { 50% { opacity: 0; } }

/* Composing preedit: underlined, not yet committed to the document. */
#preedit {
  text-decoration: underline;
  text-decoration-color: var(--accent);
  text-underline-offset: 3px;
  color: var(--accent);
}

.keyboard-shell {
  flex: 0 0 auto;
  background: var(--keyboard);
  border-top: 1px solid #c4c7cc;
  padding-bottom: env(safe-area-inset-bottom);
  font-family: "Kantumruy Pro", "Noto Sans Khmer", system-ui, sans-serif;
}

#suggestions {
  height: 32px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  align-items: center;
  padding: 2px 6px;
  border-bottom: 1px solid rgb(100 105 113 / 24%);
  overflow: hidden;
}

#suggestions .sug {
  appearance: none;
  min-width: 0;
  height: 27px;
  padding: 1px 8px;
  overflow: hidden;
  border: 0;
  border-right: 1px solid rgb(100 105 113 / 25%);
  background: transparent;
  color: #202329;
  font-family: inherit;
  font-size: 17px;
  font-weight: 500;
  line-height: 24px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#suggestions .sug:last-child { border-right: 0; }
#suggestions .sug:active { color: var(--accent); background: rgb(255 255 255 / 36%); }
#suggestions .sug.placeholder { color: #696d74; font-size: 14px; }
#suggestions .sug.default { font-weight: 700; }

.kb {
  padding: 3px 4px;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.kb-row { display: flex; gap: var(--gap); }

.key,
.action {
  position: relative;
  min-width: 0;
  appearance: none;
  border: 0;
  color: var(--ink);
  cursor: pointer;
  outline: none;
  -webkit-touch-callout: none;
}

.key {
  --heat: 0;   /* 0..1 next-key probability, set by the bigram heatmap */
  position: relative;
  flex: 1 1 0;
  height: clamp(38px, 9.5vw, 44px);
  /* --view-h: the screen height when the page is only the keyboard (ios/). */
  height: clamp(38px, calc((var(--view-h, 100dvh) * 0.33 - 90px) / 4), 44px);
  min-width: 0;
  overflow: hidden;
  border-radius: 6px;
  background: var(--key);
  box-shadow: 0 1px 0 1px var(--key-shadow), 0 2px 1px rgb(33 38 45 / 18%);
  font-family: inherit;
  font-size: clamp(18px, 4.7vw, 21px);
  font-weight: 500;
  line-height: 1;
  transition: height 140ms ease, transform 50ms ease, background 50ms ease, box-shadow 50ms ease;
  touch-action: none;
}

/* Bigram heat: a colored ring on likely-next keys, blue (cold) → red (hot).
   --heat-color is set per key in JS; --heat is 0..1 for the glow strength.
   Inset so it stays within the key's clipped, rounded bounds. */
.key::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  box-shadow: inset 0 0 0 3px var(--heat-color, transparent);
  opacity: var(--heat, 0);
  transition: opacity 120ms ease;
}

.key .member {
  position: relative;
  z-index: 0;
  isolation: isolate;
}

.key .member::after {
  content: "";
  position: absolute;
  z-index: -1;
  width: 1.6em;
  aspect-ratio: 1;
  left: 50%;
  top: 50%;
  border-radius: 50%;
  background: radial-gradient(circle, var(--member-heat-color, transparent) 0 32%, transparent 72%);
  opacity: calc(var(--member-heat, 0) * .34);
  transform: translate(-50%, -50%);
  pointer-events: none;
  transition: opacity 120ms ease;
}

.key.is-pressed {
  z-index: 2;
  background: var(--pressed);
  box-shadow: 0 1px 0 var(--key-shadow);
  transform: translateY(1px);
}

body.heat-off .key::after { opacity: 0 !important; }
body.heat-off .key .member::after { opacity: 0 !important; }
body.center-only { --gap: 3px; }
body.center-only .kb { padding-top: 3px; }
body.center-only .key { height: clamp(32px, 8.5vw, 36px); }
body.center-only .key .hint { opacity: 0; }
body.center-only .action { height: 34px; }

body.full-height { --gap: clamp(5px, 1.7vw, 8px); }
body.full-height .kb { padding: 7px 5px 5px; }
body.full-height .key {
  height: clamp(64px, 18vw, 74px);
  font-size: 26px;
}
body.full-height .key .hint {
  font-size: 16px;
  font-weight: 500;
  line-height: 1.3;
  opacity: 1;
}
body.full-height .hint.u { top: 5px; }
body.full-height .hint.d { bottom: 5px; }
body.full-height .hint.l { left: 5px; }
body.full-height .hint.r { right: 5px; }
body.full-height.center-only { --gap: clamp(4px, 1.2vw, 6px); }
body.full-height.center-only .key { height: clamp(48px, 13vw, 56px); }
body.full-height.center-only .key .hint { opacity: 0; }

body.night {
  --ink: #f2f4f7;
  --muted: #aeb4bd;
  --accent: #5aa2ff;
  --keyboard: #252930;
  --key: #3b4048;
  --key-special: #4b515b;
  --key-shadow: #171a1f;
  --pressed: #555c66;
  --page: #101216;
  --surface: #181b20;
  --composer-end: #15181d;
  --border: #30343b;
}

body.night .phone { box-shadow: 0 0 36px rgb(0 0 0 / 35%); }
body.night .keyboard-shell { border-top-color: #15181d; }
body.night .key .hint { color: #c9ced6; }
body.night #suggestions .sug { color: #eef1f5; }
body.night .app-bar .display-toggle { border-color: #555b65; }
body.night .app-bar .display-toggle[aria-checked="true"] { border-color: var(--accent); }
body.night .action.space { background: #363b43; }

.key .center { pointer-events: none; }
.key .hint {
  position: absolute;
  color: #3d4148;
  font-family: inherit;
  font-size: clamp(9px, 2.5vw, 11px);
  font-weight: 500;
  line-height: 1.15;
  white-space: nowrap;
  pointer-events: none;
}

.hint.u,
.hint.d {
  left: 25%;
  width: 50%;
  text-align: center;
}

.hint.l,
.hint.r {
  top: 50%;
  width: 27%;
  overflow: visible;
  transform: translateY(-50%);
}

.hint.u { top: 2px; }
.hint.d { bottom: 2px; }
.hint.l { left: 3px; text-align: left; }
.hint.r { right: 3px; text-align: right; }

.actions { align-items: stretch; }
.action {
  height: 36px;
  border-radius: 6px;
  background: var(--key);
  box-shadow: 0 1px 0 1px var(--key-shadow), 0 2px 1px rgb(33 38 45 / 18%);
  font: 500 13px/1 "Kantumruy Pro", system-ui, sans-serif;
}

.action:active {
  background: var(--pressed);
  box-shadow: 0 1px 0 var(--key-shadow);
  transform: translateY(1px);
}
/* Left 123 + mark, right return + delete, pairwise the same width: the space
   bar sits in the middle. */
.action.icon { flex: 0 0 clamp(42px, 12vw, 54px); font-size: 18px; }
.action.mode { flex: 0 0 clamp(52px, 15vw, 66px); }
.action.space { flex: 1 1 auto; background: var(--key); }
/* 123, return and delete: white like the letter keys, their symbol bold. */
.action.mode, .action.return, .action.icon { font-weight: 700; -webkit-text-stroke: .3px currentColor; }
.action.return { flex: 0 0 clamp(52px, 15vw, 66px); font-size: 12px; }
.action.mark { flex: 0 0 clamp(42px, 12vw, 54px); font-size: 17px; background: var(--key); }
/* The theme button: a swatch of the coeng colour on the vowel tint. */
.theme-toggle .swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--theme-coeng);
  box-shadow: 0 0 0 3px var(--theme-vowel);
  vertical-align: middle;
}
.action.mark { display: flex; align-items: center; justify-content: center; gap: .5em; }
.action.mark.is-pressed { background: var(--pressed); }
.action.mark span { pointer-events: none; }

#popup {
  position: fixed;
  z-index: 50;
  display: none;
  width: 132px;
  height: 108px;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  filter: drop-shadow(0 4px 8px rgb(18 25 38 / 22%));
}

#popup .preview-family {
  position: relative;
  width: 132px;
  height: 108px;
  border: 1px solid rgb(90 96 106 / 45%);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
  font-family: "Kantumruy Pro", "Noto Sans Khmer", system-ui, sans-serif;
  font-weight: 500;
  line-height: 1;
}

#popup .preview-member {
  position: absolute;
  min-width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-size: 21px;
}

#popup .preview-member[hidden] { display: none; }

#popup .preview-member.c { left: 49px; top: 37px; font-size: 28px; }
#popup .preview-member.u { left: 49px; top: 2px; }
#popup .preview-member.d { left: 49px; bottom: 2px; }
#popup .preview-member.l { left: 7px; top: 37px; }
#popup .preview-member.r { right: 7px; top: 37px; }
#popup .preview-member.is-selected {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 0 0 1px #0f61ca;
}

@media (max-width: 430px) {
  .app-bar {
    padding-inline: 8px;
    grid-template-columns: 28px minmax(0, 1fr) max-content;
    gap: 4px;
  }
  .header-actions { min-width: max-content; gap: 3px; }
  .header-actions > * { flex: 0 0 auto; }
  .app-bar .display-toggle { width: 28px; height: 28px; min-height: 28px; font-size: 15px; line-height: 26px; }
  .app-bar .done { min-width: 40px; font-size: 14px; }
}

@media (max-width: 380px) {
  .key .hint { font-weight: 400; opacity: .68; }
}

@media (max-width: 340px) {
  .app-bar { grid-template-columns: 28px minmax(0, 1fr); }
  .app-title { display: none; }
  .header-actions { grid-column: 2; }
  .key .hint { opacity: 0; }
}

@media (min-width: 521px) {
  body { padding-block: 18px; background: #e8eaee; }
  .phone { min-height: calc(100% - 36px); border-radius: 20px; overflow: hidden; }
}

@media (max-height: 620px) {
  .composer { padding-block: 10px; }
  #output { min-height: 48px; padding-top: 10px; }
  #suggestions { height: 30px; }
  .kb { padding-top: 3px; }
  .key { height: 38px; font-size: 18px; }
  .key .hint { font-size: 9px; }
  .action { height: 34px; }
  body.center-only .key { height: 32px; }
  body.center-only .action { height: 32px; }
}

@media (prefers-reduced-motion: reduce) {
  #caret { animation: none; }
}

/* Shown only when index.html is opened from disk instead of `npm run dev`. */
.file-warning {
  position: fixed;
  inset: 16px 16px auto;
  z-index: 100;
  margin: 0;
  padding: 12px 14px;
  border-radius: 10px;
  background: #fff3cd;
  color: #5c4400;
  font: 14px/1.45 system-ui, sans-serif;
}

/* Tap-and-guess: a tap means any letter on the key, so show them all alike. */
.key .group { display: none; }
body.guess-mode .key .center,
body.guess-mode .key .hint { visibility: hidden; }
body.guess-mode .key .group {
  position: absolute;
  inset: 2px 3px;
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: 0 .42em;
  font-size: clamp(14px, 4vw, 18px);
  font-weight: 500;
  line-height: 1.3;
  pointer-events: none;
}
body.guess-mode.full-height .key .group { font-size: clamp(18px, 5.2vw, 22px); }

/* Honeycomb (tap-and-guess): one board; each key is an SVG cell whose outline
   carries the heat ring. Touches are routed to the nearest key centre in JS. */
.honeycomb {
  position: relative;
  touch-action: none;
}
.honeycomb .key {
  position: absolute;
  inset: 0;
  width: auto;
  height: auto;
  flex: none;
  overflow: visible;
  border-radius: 0;
  background: none;
  box-shadow: none;
  transform: none;
  transition: none;
  pointer-events: none;
}
.honeycomb .key::after { display: none; }
.honeycomb .key svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  filter: drop-shadow(0 1px 0 var(--key-shadow));
}
.honeycomb .key polygon {
  fill: var(--key);
  /* The next-key hint: one colour, the theme's, stronger for a likelier key. */
  stroke: var(--theme-hint);
  stroke-opacity: calc(var(--heat, 0) * .9);
  stroke-width: 3;
  stroke-linejoin: round;
  transition: stroke-opacity 120ms ease;
}
.honeycomb .key.kind-vowel polygon { fill: var(--theme-vowel); }
.honeycomb .key.kind-coeng polygon { fill: var(--theme-coeng); }
.honeycomb .key .group,
#popup .preview-member { font-family: "Khmer Flick Keys", "Kantumruy Pro", "Noto Sans Khmer", system-ui, sans-serif; }
.honeycomb .key.kind-coeng .group { color: var(--theme-coeng-ink); }
/* Coeng, and the 123 page's signs (៑ ៎, else a dash and a dot), keep their circle. */
.honeycomb .key.kind-coeng .group,
.honeycomb .key.lean-face .group { font-family: "Kantumruy Pro", "Noto Sans Khmer", system-ui, sans-serif; }
body.heat-off .honeycomb .key polygon { stroke-opacity: 0; }
.honeycomb .key.is-pressed { transform: none; box-shadow: none; background: none; }
.honeycomb .key.is-pressed polygon { fill: var(--pressed); }
/* The 123 page: the centre, typed by a tap, large; the rest, typed by a lean, small. */
.honeycomb .key.lean-face .group .slot { font-size: .62em; opacity: .62; }
.honeycomb .key.lean-face .group .slot.c { font-size: 1.05em; opacity: 1; }
/* Each letter in its slot around the cell's centre: up, left, right, down. */
body.guess-mode .honeycomb .key .group {
  display: block;
  inset: auto;
  left: var(--cx);
  top: var(--cy);
  width: 0;
  height: 0;
}
.honeycomb .key .group .slot {
  position: absolute;
  left: 0;
  top: 0;
  line-height: 1.2;
  white-space: nowrap;
  /* --ink-dx/dy centre a vowel sign by its ink, not its empty base (keyboard.js). */
  transform: translate(calc(-50% + var(--ink-dx, 0px)), calc(-50% + var(--ink-dy, 0px)));
}
.honeycomb .key .group .slot.u { top: calc(-1 * var(--ring-y)); }
.honeycomb .key .group .slot.d { top: var(--ring-y); }
.honeycomb .key .group .slot.l { left: calc(-1 * var(--ring-x)); }
.honeycomb .key .group .slot.r { left: var(--ring-x); }

/* Inside the iOS keyboard extension (ios/, see ui/host.js): only the keyboard,
   under a slim bar with the globe key and the layout switches. */
html.host body { background: var(--keyboard); }
html.host .phone { min-height: 0; max-width: none; box-shadow: none; background: var(--keyboard); }
html.host .composer { display: none; }
html.host .app-bar {
  /* Never squeezed: the height reported to iOS must be the natural one. */
  flex: none;
  min-height: 0;
  padding: 3px 8px 0;
  grid-template-columns: 34px minmax(0, 1fr) auto;
  border-bottom: 0;
  background: var(--keyboard);
}
html.host #next-keyboard { grid-column: 1; display: grid; place-items: center; color: var(--ink); }
html.host #next-keyboard[hidden] { display: none; }
html.host .app-title { grid-column: 2; visibility: hidden; }
html.host .header-actions { grid-column: 3; }
html.host .app-bar .back,
html.host .app-bar .done,
html.host #night-toggle { display: none; }
html.host .keyboard-shell { border-top: 0; }
