:root {
  --bg: #edf7f3;
  --surface: #ffffff;
  --ink: #243240;
  --muted: #5d6b73;
  --selected: #facc15;
  --speaking: #14b8a6;
  --pressed: #f97316;
  --control-bg: rgba(255, 255, 255, 0.78);
}

* {
  box-sizing: border-box;
}

html,
body {
  background: var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  margin: 0;
  min-height: 100vh;
  overscroll-behavior: none;
}

button {
  cursor: pointer;
  font: inherit;
}

.aac-shell {
  display: grid;
  gap: 16px;
  min-height: 100vh;
  outline: none;
  padding: clamp(16px, 2.2vw, 34px);
  padding-bottom: clamp(136px, 15vh, 178px);
}

.loading-screen {
  align-content: center;
  display: grid;
  min-height: 100vh;
  text-align: center;
}

.aac-header {
  align-items: center;
  display: flex;
  gap: 18px;
  justify-content: space-between;
}

.eyebrow {
  color: var(--muted);
  font-size: 13px;
  font-weight: 900;
  letter-spacing: 0.08em;
  margin: 0 0 4px;
  text-transform: uppercase;
}

h1 {
  font-size: clamp(32px, 5vw, 68px);
  line-height: 1;
  margin: 0;
}

.status-line {
  color: var(--muted);
  font-size: clamp(18px, 2vw, 28px);
  font-weight: 800;
  margin: 10px 0 0;
}

.nav-controls {
  display: flex;
  gap: 12px;
}

.nav-controls button {
  background: var(--ink);
  border: 0;
  border-radius: 12px;
  color: #ffffff;
  font-size: clamp(18px, 1.8vw, 26px);
  font-weight: 900;
  min-height: 64px;
  min-width: 112px;
  padding: 14px 22px;
}

.nav-controls button:disabled {
  background: #aeb7bd;
  cursor: default;
}

/* Settings gear — small, unobtrusive button in nav-controls */
.settings-gear {
  background: transparent !important;
  border: 2px solid rgba(255, 255, 255, 0.4) !important;
  border-radius: 50% !important;
  color: #ffffff !important;
  font-size: 22px !important;
  min-height: 48px !important;
  min-width: 48px !important;
  padding: 0 !important;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

/* Overlay backdrop — hidden until settingsOpen */
.settings-overlay {
  align-items: center;
  background: rgba(0, 0, 0, 0.55);
  bottom: 0;
  display: flex;
  justify-content: center;
  left: 0;
  position: fixed;
  right: 0;
  top: 0;
  z-index: 200;
}

.settings-overlay[hidden] {
  display: none;
}

/* Settings panel inside the overlay */
.display-controls {
  align-items: stretch;
  background: var(--bg, #f6f3ee);
  border-radius: 24px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  gap: 20px;
  max-width: 520px;
  padding: 28px 28px 24px;
  width: 90vw;
}

.settings-header {
  align-items: center;
  display: flex;
  justify-content: space-between;
}

.settings-title {
  font-size: clamp(18px, 1.8vw, 24px);
  font-weight: 900;
}

.settings-close {
  background: transparent;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  font-size: 20px;
  line-height: 1;
  min-height: 44px;
  min-width: 44px;
  padding: 0;
}

.settings-footer {
  border-top: 1px solid rgba(36, 50, 64, 0.1);
  padding-top: 16px;
}

.settings-pair-btn {
  background: transparent;
  border: 2px solid var(--ink);
  border-radius: 10px;
  color: var(--ink);
  cursor: pointer;
  font-size: clamp(15px, 1.4vw, 20px);
  font-weight: 700;
  min-height: 48px;
  padding: 10px 20px;
  width: 100%;
}

.segmented-control,
.setting-toggles {
  background: var(--control-bg);
  border: 2px solid rgba(36, 50, 64, 0.1);
  border-radius: 12px;
  display: flex;
  gap: 6px;
  padding: 6px;
}

.segmented-control button,
.setting-toggles button {
  background: transparent;
  border: 0;
  border-radius: 8px;
  color: var(--ink);
  font-size: clamp(16px, 1.4vw, 21px);
  font-weight: 900;
  min-height: 48px;
  min-width: 58px;
  padding: 8px 14px;
}

.segmented-control button[aria-pressed="true"],
.setting-toggles button[aria-pressed="true"] {
  background: var(--ink);
  color: #ffffff;
}

.tile-grid {
  display: grid;
  gap: clamp(14px, 2vw, 28px);
  grid-template-columns: repeat(var(--columns), minmax(0, 1fr));
}

.tile {
  aspect-ratio: 1;
  background: var(--surface);
  border: 6px solid rgba(255, 255, 255, 0.76);
  border-radius: 16px;
  box-shadow: 0 14px 34px rgba(36, 50, 64, 0.16);
  min-height: 168px;
  overflow: hidden;
  padding: 0;
  position: relative;
  transition: border-color 120ms ease, box-shadow 120ms ease, transform 120ms ease;
}

.tile img {
  height: 100%;
  inset: 0;
  object-fit: cover;
  position: absolute;
  width: 100%;
}

.tile::after {
  background: linear-gradient(transparent 36%, rgba(0, 0, 0, 0.78));
  content: "";
  inset: 0;
  position: absolute;
}

.tile span {
  bottom: 0;
  color: #ffffff;
  display: block;
  font-size: clamp(28px, 4vw, 58px);
  font-weight: 1000;
  left: 0;
  line-height: 1.04;
  padding: clamp(14px, 2vw, 26px);
  position: absolute;
  right: 0;
  text-align: center;
  z-index: 1;
}

.labels-hidden .tile span {
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  height: 1px;
  overflow: hidden;
  padding: 0;
  white-space: nowrap;
  width: 1px;
}

.labels-hidden .tile::after {
  background: linear-gradient(transparent 72%, rgba(0, 0, 0, 0.42));
}

.tile.selected {
  border-color: var(--selected);
  box-shadow: 0 0 0 8px rgba(250, 204, 21, 0.22), 0 14px 34px rgba(36, 50, 64, 0.18);
}

.tile.pressed {
  border-color: var(--pressed);
  transform: scale(0.975);
}

.tile.speaking {
  border-color: var(--speaking);
  box-shadow: 0 0 0 10px rgba(20, 184, 166, 0.22), 0 18px 38px rgba(36, 50, 64, 0.2);
}

.tile-fallback {
  align-items: center;
  background: linear-gradient(135deg, #2f6f7e, #84563c);
  color: #ffffff;
  display: flex;
  font-size: clamp(24px, 3vw, 44px);
  font-weight: 900;
  height: 100%;
  justify-content: center;
  padding: 20px;
  text-align: center;
  width: 100%;
}

/* System-injected Back tile (reserved grid cell 1, ADR-0018): visually consistent
   and distinct from authored tiles on every sub-board render. */
.system-back-tile .tile-fallback {
  background: linear-gradient(135deg, #0f766e, #134e4a);
}

.system-back-tile .tile-fallback::before {
  content: "\2190";
  font-size: 1.4em;
  margin-right: 0.2em;
}

.phrase-dock {
  align-items: center;
  background: #243240;
  border-top: 6px solid rgba(20, 184, 166, 0.54);
  bottom: 0;
  color: #ffffff;
  display: flex;
  gap: 18px;
  justify-content: space-between;
  left: 0;
  min-height: 116px;
  padding: clamp(16px, 2vw, 24px) clamp(18px, 3vw, 42px);
  position: fixed;
  right: 0;
  z-index: 5;
}

.phrase-dock.speaking {
  border-top-color: var(--speaking);
}

.phrase-state {
  color: #b9f3e8;
  font-size: clamp(15px, 1.5vw, 20px);
  font-weight: 900;
  margin: 0 0 4px;
  text-transform: uppercase;
}

.phrase-text {
  font-size: clamp(28px, 4vw, 54px);
  font-weight: 1000;
  line-height: 1.02;
  margin: 0;
}

.phrase-actions {
  display: flex;
  gap: 12px;
}

.phrase-actions button {
  background: #ffffff;
  border: 0;
  border-radius: 12px;
  color: var(--ink);
  font-size: clamp(16px, 1.5vw, 22px);
  font-weight: 900;
  min-height: 58px;
  min-width: 112px;
  padding: 12px 18px;
}

.phrase-actions button:disabled {
  background: #b8c4c7;
  color: #536167;
  cursor: default;
}

.high-contrast {
  --bg: #000000;
  --control-bg: #ffffff;
  --ink: #ffffff;
  --muted: #fef08a;
  --selected: #fde047;
  --speaking: #5eead4;
  --surface: #111827;
  color: #ffffff;
}

.high-contrast .segmented-control,
.high-contrast .setting-toggles {
  border-color: #ffffff;
}

.high-contrast .segmented-control button,
.high-contrast .setting-toggles button {
  color: #000000;
}

.high-contrast .segmented-control button[aria-pressed="true"],
.high-contrast .setting-toggles button[aria-pressed="true"] {
  background: #facc15;
  color: #000000;
}

.high-contrast .nav-controls button {
  background: #facc15;
  color: #000000;
}

.high-contrast .nav-controls button:disabled {
  background: #4b5563;
  color: #ffffff;
}

.high-contrast .tile {
  border-color: #ffffff;
  box-shadow: none;
}

.high-contrast .tile.selected,
.high-contrast .tile.speaking {
  box-shadow: 0 0 0 10px rgba(250, 204, 21, 0.38);
}

.high-contrast .phrase-dock {
  background: #000000;
  border-top-color: #facc15;
}

.high-contrast .phrase-state {
  color: #fef08a;
}

.high-contrast .phrase-actions button {
  background: #facc15;
  color: #000000;
}

.reduced-motion .tile {
  transition: none;
}

.reduced-motion .tile.pressed {
  transform: none;
}

@media (max-width: 760px) {
  .aac-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .nav-controls {
    width: 100%;
  }

  .nav-controls button {
    flex: 1;
  }

  .display-controls {
    align-items: stretch;
    flex-direction: column;
  }

  .segmented-control,
  .setting-toggles {
    overflow-x: auto;
  }

  .phrase-dock {
    align-items: stretch;
    flex-direction: column;
  }

  .phrase-actions button {
    flex: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tile {
    transition: none;
  }

  .tile.pressed {
    transform: none;
  }
}

/* Offline dot — amber circle in header nav-controls */
.offline-dot {
  background: #f59e0b;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
  height: 14px;
  width: 14px;
}

/* Re-pair notice — non-blocking inline note below status-line */
.repair-notice {
  color: var(--muted);
  font-size: clamp(14px, 1.4vw, 18px);
  font-weight: 700;
  margin: 6px 0 0;
}

.repair-pair-btn {
  background: none;
  border: none;
  color: var(--ink);
  cursor: pointer;
  font: inherit;
  font-weight: 900;
  padding: 0;
  text-decoration: underline;
}

/* Pairing screen */
.pairing-screen {
  align-content: center;
  display: grid;
  justify-items: center;
  min-height: 100vh;
  padding: clamp(24px, 5vw, 64px);
}

.pairing-card {
  background: var(--surface);
  border-radius: 20px;
  box-shadow: 0 14px 48px rgba(36, 50, 64, 0.14);
  display: grid;
  gap: 18px;
  max-width: 480px;
  padding: clamp(28px, 5vw, 52px);
  width: 100%;
}

.pairing-card h1 {
  font-size: clamp(28px, 5vw, 48px);
  margin: 4px 0 0;
}

.pairing-instruction {
  color: var(--muted);
  font-size: clamp(17px, 1.8vw, 22px);
  font-weight: 600;
  margin: 0;
}

.pairing-code-input {
  background: var(--bg);
  border: 3px solid rgba(36, 50, 64, 0.18);
  border-radius: 12px;
  color: var(--ink);
  font-family: ui-monospace, "Cascadia Code", "Courier New", monospace;
  font-size: clamp(28px, 5vw, 48px);
  font-weight: 900;
  letter-spacing: 0.2em;
  padding: 14px 18px;
  text-align: center;
  text-transform: uppercase;
  width: 100%;
}

.pairing-code-input:focus {
  border-color: var(--ink);
  outline: 3px solid rgba(36, 50, 64, 0.22);
  outline-offset: 2px;
}

.pairing-code-input:disabled {
  opacity: 0.55;
}

.pairing-status {
  color: var(--muted);
  font-size: clamp(15px, 1.5vw, 19px);
  font-weight: 700;
  margin: 0;
  min-height: 1.6em;
}

.pairing-status.verifying {
  color: var(--ink);
}

.pairing-status.error {
  color: #dc2626;
}

.pairing-connect-btn {
  background: var(--ink);
  border: 0;
  border-radius: 12px;
  color: #ffffff;
  font-size: clamp(18px, 1.8vw, 24px);
  font-weight: 900;
  min-height: 62px;
  padding: 14px 28px;
  width: 100%;
}

.pairing-connect-btn:disabled {
  background: #aeb7bd;
  cursor: default;
}

.high-contrast .pairing-card {
  background: #111827;
  box-shadow: none;
}

.high-contrast .pairing-code-input {
  background: #000000;
  border-color: #ffffff;
  color: #ffffff;
}

.high-contrast .pairing-connect-btn {
  background: #facc15;
  color: #000000;
}
