:root {
  --bg: #0d1420;
  --panel: #162132;
  --tile: #1f2d44;
  --tile-hi: #2a3d5c;
  --text: #f3f6fb;
  --muted: #9fb0c8;
  --accent: #0399ff;
  --yes: #1d7f47;
  --no: #a8322a;
  --warn: #9a6a00;
  --calm: #4b3d7a;
  --ring: #ffd34d;
  --gap: 14px;
  --cols: 4;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
  user-select: none;
}

#app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  gap: var(--gap);
  padding: var(--gap);
}

/* ---------- top message bar ---------- */
.bar {
  display: grid;
  grid-template-columns: 1fr minmax(130px, 11vw) minmax(130px, 11vw);
  gap: var(--gap);
  height: clamp(90px, 14vh, 150px);
}

.message {
  background: var(--panel);
  border-radius: 18px;
  padding: 10px 24px;
  font-size: clamp(28px, 3.6vw, 60px);
  display: flex;
  align-items: center;
  overflow: hidden;
  white-space: nowrap;
  direction: rtl;           /* keep the END of a long message visible */
  text-align: left;
}
.message span { direction: ltr; unicode-bidi: plaintext; }
.message.empty { color: var(--muted); }
.message.speaking { box-shadow: inset 0 0 0 4px var(--accent); }
.message .caret {
  display: inline-block; width: 3px; height: 1em; background: var(--accent);
  margin-left: 4px; vertical-align: -0.12em; animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ---------- pages ---------- */
.page {
  display: grid;
  gap: var(--gap);
  min-height: 0;
}
.page.tiles {
  grid-template-columns: repeat(var(--cols), 1fr);
  grid-auto-rows: 1fr;
}
.page.keyboard {
  grid-template-columns: repeat(20, 1fr);
  grid-template-rows: repeat(6, 1fr);
}

/* ---------- dwell targets ---------- */
.dwell {
  position: relative;
  border: 0;
  border-radius: 18px;
  background: var(--tile);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  font-size: clamp(20px, 2.3vw, 40px);
  line-height: 1.15;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
  padding: 8px;
  overflow: hidden;
  min-width: 0;
  min-height: 0;
  cursor: default;
}
.dwell::before {               /* fill that rises while dwelling */
  content: "";
  position: absolute; inset: auto 0 0 0;
  height: calc(var(--p, 0) * 100%);
  background: rgba(255, 211, 77, 0.28);
  pointer-events: none;
}
.dwell::after {                /* progress bar along the bottom */
  content: "";
  position: absolute; left: 0; bottom: 0;
  height: 10px;
  width: calc(var(--p, 0) * 100%);
  background: var(--ring);
  pointer-events: none;
}
.dwell.hover { outline: 4px solid var(--ring); outline-offset: -4px; }
.dwell.fired { animation: fired 0.45s ease-out; }
@keyframes fired { from { background: var(--ring); color: #000; } }
.dwell[disabled] { opacity: 0.35; }
.dwell .icon { font-size: 1.9em; line-height: 1; font-family: "Segoe UI Emoji", "Apple Color Emoji", "Noto Color Emoji", sans-serif; }
.dwell .label { position: relative; }
.show-icons .dwell.tile .label { display: none; }
.show-words .dwell.tile .icon { display: none; }
.show-icons .dwell.tile .icon { font-size: 3.2em; }

.c-yes  { background: var(--yes); }
.c-no   { background: var(--no); }
.c-warn { background: var(--warn); }
.c-calm { background: var(--calm); }
.c-blue { background: #1c4f8a; }
.speak  { background: var(--accent); color: #fff; }

.key { font-size: clamp(22px, 2.6vw, 44px); }
.suggest { background: #22364f; font-size: clamp(18px, 1.8vw, 32px); font-weight: 500; }
.suggest.phrase { background: #263a2c; }
.suggest:empty, .suggest.blank { visibility: hidden; }

/* ---------- bottom nav ---------- */
.nav {
  display: grid;
  grid-template-columns: repeat(3, 1fr) 90px;
  gap: var(--gap);
  height: clamp(70px, 10vh, 110px);
}
.nav .dwell.current { background: var(--tile-hi); box-shadow: inset 0 0 0 3px var(--accent); }
.gear {
  border: 0; border-radius: 18px; background: var(--panel); color: var(--muted);
  font-size: 34px; cursor: pointer;
}

/* ---------- rest mode ---------- */
body.paused .page, body.paused .bar .dwell, body.paused .nav .dwell:not([data-pause-ok]) { opacity: 0.18; }
body.paused [data-pause-ok] { background: var(--calm); }
.rest-banner {
  display: none; position: fixed; left: 50%; top: 45%; transform: translate(-50%, -50%);
  font-size: clamp(30px, 4vw, 64px); color: var(--muted); pointer-events: none; text-align: center;
}
body.paused .rest-banner { display: block; }

/* ---------- start overlay ---------- */
.start {
  position: fixed; inset: 0; background: rgba(5, 9, 15, 0.92);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.start button {
  font: 600 40px "Segoe UI", sans-serif; padding: 40px 80px; border-radius: 24px; border: 0;
  background: var(--accent); color: #fff; cursor: pointer;
}
.start p { position: absolute; bottom: 12%; color: var(--muted); font-size: 20px; text-align: center; padding: 0 16px; }

/* ---------- settings (caregiver; mouse/touch only, never dwell) ---------- */
.settings {
  position: fixed; inset: 0; background: var(--bg); z-index: 40; overflow: auto;
  padding: 24px clamp(16px, 4vw, 60px) 80px; font-size: 18px; user-select: text;
}
.settings h2 { margin: 28px 0 12px; font-size: 22px; color: var(--accent); }
.settings h1 { margin: 0; display: flex; justify-content: space-between; align-items: center; }
.settings label { display: flex; gap: 12px; align-items: center; margin: 10px 0; flex-wrap: wrap; }
.settings input[type=range] { width: min(380px, 60vw); }
.settings input[type=text], .settings select, .settings textarea {
  font: inherit; padding: 8px 10px; border-radius: 8px; border: 1px solid #33465f;
  background: var(--panel); color: var(--text);
}
.settings button {
  font: inherit; padding: 8px 16px; border-radius: 8px; border: 0;
  background: var(--tile-hi); color: var(--text); cursor: pointer;
}
.settings button.primary { background: var(--accent); }
.settings button.danger { background: var(--no); }
.tile-row { display: grid; grid-template-columns: 70px 1fr 2fr 110px 80px auto; gap: 8px; align-items: center; margin: 6px 0; }
.tile-row input.icon-in { text-align: center; font-size: 26px; }
@media (max-width: 800px) {
  .tile-row {
    grid-template-columns: 64px 1fr auto auto;
    grid-template-areas: "icon label label label" "say say say say" "color color chime btns";
    background: var(--panel); padding: 10px; border-radius: 12px; margin: 10px 0;
  }
  .tile-row > .icon-in { grid-area: icon; }
  .tile-row > [data-f=label] { grid-area: label; }
  .tile-row > [data-f=say] { grid-area: say; }
  .tile-row > select { grid-area: color; }
  .tile-row > label { grid-area: chime; flex-wrap: nowrap; white-space: nowrap; gap: 6px; }
  .tile-row > span { grid-area: btns; justify-self: end; }
  .tile-row input, .tile-row select { width: 100%; min-width: 0; }
}
.hint { color: var(--muted); font-size: 15px; }

.voice-group h3 { margin: 14px 0 6px; font-size: 17px; color: var(--muted); font-weight: 600; }
.voice-row { display: flex; gap: 12px; align-items: center; padding: 6px 10px; border-radius: 10px; }
.voice-row.chosen { background: var(--panel); box-shadow: inset 0 0 0 2px var(--accent); }
.voice-row button { width: 44px; height: 44px; font-size: 18px; }

/* ---------- linking, notices, PIN ---------- */
.notice {
  position: fixed; left: 50%; top: 18px; transform: translateX(-50%); z-index: 60;
  background: var(--panel); border: 2px solid var(--ring); border-radius: 12px;
  padding: 12px 18px; font-size: 18px; max-width: min(700px, calc(100vw - 32px));
}
.unlinked {
  position: fixed; right: 16px; bottom: calc(clamp(70px, 10vh, 110px) + 30px); z-index: 30;
  background: var(--panel); color: var(--muted); border-radius: 10px; padding: 8px 14px; font-size: 15px;
}
.unlinked { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; max-width: min(760px, calc(100vw - 32px)); }
.unlinked input { font: inherit; padding: 6px 10px; border-radius: 8px; border: 1px solid #33465f; background: var(--bg); color: var(--text); min-width: 260px; }
.unlinked button { font: inherit; padding: 6px 12px; border-radius: 8px; border: 0; background: var(--accent); color: #fff; cursor: pointer; }
.unlinked a, .settings a.btn { color: var(--accent); }
.settings a.btn {
  font-size: 16px; text-decoration: none; padding: 8px 14px; border-radius: 8px; background: var(--tile-hi); color: var(--text);
}
.settings h1 { gap: 12px; flex-wrap: wrap; }
.pin {
  position: fixed; inset: 0; z-index: 70; background: rgba(5, 9, 15, 0.9);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.pin form { background: var(--panel); border-radius: 16px; padding: 24px; width: min(360px, 100%); text-align: center; }
.pin h2 { margin: 0 0 14px; }
.pin input {
  font: 600 34px "Segoe UI", sans-serif; letter-spacing: 0.4em; text-align: center; width: 100%;
  padding: 10px; border-radius: 10px; border: 1px solid #33465f; background: var(--bg); color: var(--text);
}
.pin button { font: inherit; font-size: 18px; padding: 10px 18px; border-radius: 10px; border: 0; background: var(--tile-hi); color: var(--text); cursor: pointer; margin-top: 8px; }
.pin button.primary { background: var(--accent); }
