:root {
  --bg: #0e1116;
  --panel: #161b22;
  --border: #2a3038;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --good: #34d399;
  --warn: #fbbf24;
  --bad: #f87171;
  --stimulus: #34d399;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
  touch-action: manipulation;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* Блокируем выделение и жесты во время теста */
body.testing {
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  overflow: hidden;
}

.wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  position: relative;
}

h1 { font-size: 1.6rem; margin: 0 0 8px; font-weight: 700; }
h2 { font-size: 1.2rem; margin: 0 0 16px; font-weight: 600; }
p  { color: var(--muted); line-height: 1.6; margin: 0 0 16px; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  max-width: 560px;
  width: 100%;
}

.instruction {
  font-size: 1.05rem;
  color: var(--text);
  line-height: 1.7;
  margin: 0 0 24px;
}

button {
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  padding: 14px 28px;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}
.btn-ghost:hover { color: var(--text); border-color: var(--muted); }

.btn-danger { background: transparent; color: var(--bad); border: 1px solid var(--bad); }
.btn-danger:hover { background: rgba(248,113,113,0.1); }

.row { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }

/* Breadcrumb "back home" in the top-left corner */
.breadcrumb {
  position: absolute;
  top: 20px; left: 20px;
  display: inline-flex; align-items: center; gap: 6px;
  color: var(--muted); text-decoration: none;
  font-size: 0.95rem; font-weight: 600;
  transition: color 0.15s;
  z-index: 2;
}
.breadcrumb:hover { color: var(--text); }
.breadcrumb .arrow { font-size: 1.15rem; line-height: 1; margin-top: -1px; }

/* ---------- Главное меню ---------- */
.menu {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 8px;
}
.menu .btn-menu {
  display: block;
  width: 100%;
  text-decoration: none;
  text-align: center;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 18px 24px;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s;
}
.menu .btn-menu:hover { background: var(--accent-hover); }
.menu .btn-menu.secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
.menu .btn-menu.secondary:hover { border-color: var(--muted); }

/* Status badge on the Google-auth settings button */
.settings-status {
  display: inline-block;
  margin-right: 8px;
  font-weight: 700;
  line-height: 1;
}
.settings-status:empty { display: none; }
.settings-status.good { color: var(--good); }
.settings-status.warn { color: var(--warn); }
.settings-status.bad  { color: var(--bad); }

/* Collapsible test settings on the test intro card */
.settings-details {
  text-align: left;
  margin: 20px 0 8px;
  border-top: 1px solid var(--border);
}
.settings-details > summary {
  cursor: pointer;
  list-style: none;
  user-select: none;
  -webkit-user-select: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding-top: 16px;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
}
.settings-details > summary::-webkit-details-marker { display: none; }
.settings-details > summary::before {
  content: '▸';
  display: inline-block;
  font-size: 0.9rem;
  transition: transform 0.15s;
}
.settings-details[open] > summary::before { transform: rotate(90deg); }
.settings-details > summary:hover { color: var(--text); }

/* Pre-test survey (sleep / alcohol / clarity) on the intro card */
.pre-survey {
  text-align: left;
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.settings-modal { max-width: 460px; text-align: left; }
.settings-modal h2 { text-align: center; }
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 16px;
}
.setting-row label { font-size: 0.95rem; color: var(--text); }
.setting-input { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.setting-input input {
  width: 90px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font-family: inherit;
  font-size: 0.95rem;
  text-align: center;
}
.setting-hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin: 6px 0 0;
  line-height: 1.4;
}
.setting-hint a { color: var(--accent); text-decoration: none; }
.setting-hint a:hover { text-decoration: underline; }
.setting-col {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 14px;
}
.setting-col label { font-size: 0.95rem; color: var(--text); }
.setting-col input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 0.9rem;
}
.settings-divider {
  height: 1px;
  background: var(--border);
  margin: 22px 0 0;
}
.settings-subhead {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
  margin: 16px 0 2px;
}
.send-status { font-size: 0.9rem; margin-top: 12px; min-height: 1.2em; }
.send-status.ok  { color: var(--good); }
.send-status.err { color: var(--bad); }
.send-status.pending { color: var(--muted); }

/* Игровое поле */
#stage {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  background: var(--bg);
  z-index: 10;
  cursor: pointer;
}
#stage.active { display: flex; }

.marker {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--border);
  opacity: 0.5;
}

.countdown {
  font-size: 6rem;
  font-weight: 800;
  color: var(--accent);
}

.timer {
  font-size: clamp(4rem, 18vw, 9rem);
  font-weight: 800;
  color: var(--stimulus);
  font-variant-numeric: tabular-nums;
  letter-spacing: -2px;
}

.feedback {
  font-size: clamp(3rem, 14vw, 7rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.feedback.rt   { color: var(--text); }
.feedback.early{ color: var(--bad); font-size: clamp(2rem, 9vw, 4rem); }
.feedback.miss { color: var(--warn); font-size: clamp(2rem, 9vw, 4rem); }

.hud {
  position: fixed;
  top: 0; left: 0; right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  font-size: 0.9rem;
  color: var(--muted);
  z-index: 11;
  pointer-events: none;
}
.hud .remaining { font-variant-numeric: tabular-nums; }
.hud button { pointer-events: auto; padding: 8px 16px; font-size: 0.85rem; }

/* Опрос / контекст */
.survey-q { text-align: left; margin-top: 18px; }
.survey-q .q-label { font-size: 0.98rem; color: var(--text); margin-bottom: 8px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 500;
}
.chip:hover { border-color: var(--muted); }
.chip.selected { background: var(--accent); border-color: var(--accent); color: #fff; }
.survey-comment {
  width: 100%;
  min-height: 70px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 0.9rem;
  resize: vertical;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin: 20px 0;
}
.metric {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  text-align: left;
}
.metric .label { font-size: 0.78rem; color: var(--muted); margin-bottom: 6px; }
.metric .value { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.metric .unit  { font-size: 0.85rem; color: var(--muted); font-weight: 400; }

.section-title {
  text-align: left;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
  margin: 24px 0 4px;
}

.hidden { display: none !important; }

/* Stacked blocks on the result screen */
.result-stack { width: 100%; max-width: 720px; display: flex; flex-direction: column; gap: 16px; }
.save-block { text-align: center; }
.save-block .btn-primary { min-width: 220px; }

/* Карточные экраны */
.screen { display: none; }
.screen.active { display: block; }
.wrap.screen.active { display: flex; }

.summary-list { text-align: left; margin: 16px 0; }
.summary-list .row-item {
  display: flex; justify-content: space-between;
  padding: 10px 0; border-bottom: 1px solid var(--border);
  font-size: 1rem;
}
.summary-list .row-item:last-child { border-bottom: none; }
.summary-list .row-item .k { color: var(--muted); }
.summary-list .row-item .v { font-weight: 700; font-variant-numeric: tabular-nums; }

/* DSST — игровое поле */
#dsstStage {
  position: fixed; inset: 0; display: none;
  flex-direction: column; align-items: center;
  background: var(--bg); z-index: 10;
  padding: 60px 16px 24px;
}
#dsstStage.active { display: flex; }

.dsst-legend {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px; width: 100%; max-width: 640px;
}
.dsst-legend .cell {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 0; text-align: center;
}
.dsst-legend .d { font-size: 0.85rem; color: var(--muted); }
.dsst-legend .s { font-size: 1.4rem; line-height: 1.2; }

.dsst-middle {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 18px;
}
.dsst-row {
  display: flex; gap: 14px; justify-content: center;
  align-items: center; flex-wrap: wrap;
}
.dsst-cell { display: flex; flex-direction: column; align-items: center; min-width: 42px; }
.dsst-cell .digit {
  font-size: clamp(2rem, 6vw, 3rem); font-weight: 800; line-height: 1.1;
  padding: 2px 10px; border-radius: 8px; font-variant-numeric: tabular-nums;
}
.dsst-cell.current .digit {
  background: #fde047; color: #111;
  font-size: clamp(3rem, 10vw, 5rem);
  line-height: 1;
  /* Digits sit high in the font box (tall ascent, ~zero descent), so equal
     padding leaves the glyph looking low. Trim the top and pad the bottom to
     seat it in the optical center of the badge. In em, so it scales with the
     clamped font-size. */
  padding: 0.08em 0.15em 0.125em;
}
.dsst-cell.empty { visibility: hidden; }
.dsst-cell .slot {
  font-size: clamp(1.4rem, 4.5vw, 2rem); height: 2.2rem; margin-top: 10px; color: var(--text);
}
.dsst-phase { color: var(--muted); font-size: 0.95rem; min-height: 1.2em; text-align: center; }

.dsst-buttons button.wrong { border-color: var(--bad); color: var(--bad); }
.dsst-buttons button.wrong::after {
  content: '✕'; position: absolute; top: 2px; right: 5px;
  font-size: 0.85rem; color: var(--bad); font-weight: 700;
}

.dsst-buttons {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 8px; width: 100%; max-width: 640px;
}
.dsst-buttons button {
  position: relative;
  background: var(--panel); border: 1px solid var(--border);
  color: var(--text); border-radius: 10px;
  padding: 15px 10px; font-size: 1.6rem;
  display: flex; align-items: center; justify-content: center;
}
.dsst-buttons button:hover { border-color: var(--muted); }
@media (max-width: 560px) {
  .dsst-legend .s { font-size: 1.05rem; }
  .dsst-legend .d { font-size: 0.7rem; }
  .dsst-buttons button { font-size: 1.2rem; padding: 13px 8px; }
}

/* Модалка */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 24px;
}
.modal-backdrop.active { display: flex; }
.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  max-width: 360px;
  width: 100%;
  text-align: center;
}
.modal.settings-modal { max-width: 460px; text-align: left; max-height: 90vh; overflow-y: auto; }

/* Оверлей «сохранено» */
.toast-overlay {
  position: fixed; inset: 0;
  background: rgba(14,17,22,0.85);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 30;
}
.toast-overlay.active { display: flex; }
.toast-overlay .toast {
  font-size: 1.3rem; font-weight: 700; color: var(--good);
}

/* ---------- Analytics screen (standalone PVT / DSST page) ---------- */
#analytics {
  justify-content: flex-start;
  align-items: stretch;
  padding-top: 56px;
}
/* "Show my history" prompt (shown when no OAuth token is cached yet) */
.load-prompt { display: flex; flex-direction: column; align-items: flex-start; gap: 10px; }
.load-prompt .load-hint { color: var(--muted); font-size: 0.85rem; }

/* test switcher tabs */
.tab-row { display: flex; gap: 8px; }
.tab {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--muted);
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.tab:hover { border-color: var(--muted); }
.tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.analytics-stack {
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
  text-align: left;
}
.analytics-head h1 { margin-bottom: 4px; }
.analytics-sub { color: var(--muted); font-size: 0.85rem; line-height: 1.5; }
.analytics-error {
  background: rgba(248,113,113,0.1);
  border: 1px solid var(--bad);
  color: var(--bad);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 0.9rem;
}
.baseline-notice {
  background: var(--panel);
  border: 1px solid var(--border);
  border-left: 3px solid var(--warn);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 0.92rem;
  color: var(--text);
}

/* latest-result cards */
.acards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}
.acard {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
}
.acard-label { font-size: 0.78rem; color: var(--muted); margin-bottom: 6px; }
.acard-value { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.acard-value .unit { font-size: 0.85rem; color: var(--muted); font-weight: 400; }
.acard-dev { font-size: 0.8rem; color: var(--muted); margin-top: 6px; }
.acard-dev.dev-bad  { color: var(--bad); }
.acard-dev.dev-good { color: var(--good); }

/* shared analytics controls: date range (left) + aggregation window (right) */
.controls-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px 16px; flex-wrap: wrap;
}
.period-row { display: flex; gap: 8px; flex-wrap: wrap; }

.agg-control { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.agg-label { font-size: 0.85rem; color: var(--muted); }
.agg-input { display: flex; align-items: center; gap: 6px; }
.agg-input input {
  width: 60px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 8px;
  font-family: inherit;
  font-size: 0.9rem;
  text-align: center;
}
.agg-suffix { font-size: 0.8rem; color: var(--muted); white-space: nowrap; }

/* chart cards — stack with the same rhythm as the analytics stack */
#chartsHost { display: flex; flex-direction: column; gap: 16px; }
.chart-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
}
.chart-title {
  font-size: 0.98rem; font-weight: 600; margin-bottom: 10px;
  display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap;
}
.chart-hint { font-size: 0.75rem; color: var(--muted); font-weight: 400; }
.chart-host { position: relative; width: 100%; }

/* SVG chart internals */
.mmc-svg { width: 100%; height: auto; display: block; }
.mmc-empty { color: var(--muted); font-size: 0.9rem; padding: 24px 0; text-align: center; }
.mmc-grid { stroke: var(--border); stroke-width: 1; opacity: 0.5; }
.mmc-tick-label { fill: var(--muted); font-size: 12px; font-family: inherit; }
.mmc-band { fill: var(--accent); opacity: 0.12; }
.mmc-baseline { stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 5 4; opacity: 0.8; }
.mmc-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.mmc-trend { stroke: var(--accent); stroke-width: 2.5; stroke-dasharray: 7 5; stroke-linecap: round; opacity: 0.9; }
.mmc-point { fill: var(--accent); stroke: var(--bg); stroke-width: 1.5; cursor: pointer; }
.mmc-point.incompat { fill: var(--bg); stroke: var(--muted); stroke-width: 1.5; }
.mmc-point.agg { r: 5; }
.mmc-point.active { r: 6; }

.mmc-tooltip {
  position: absolute;
  z-index: 5;
  min-width: 160px;
  max-width: 240px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.82rem;
  color: var(--text);
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  pointer-events: none;
}
.mmc-tip-date { color: var(--muted); font-size: 0.78rem; margin-bottom: 4px; }
.mmc-tip-val { font-size: 1.05rem; font-weight: 700; margin-bottom: 6px; font-variant-numeric: tabular-nums; }
.mmc-tip-row { display: flex; justify-content: space-between; gap: 12px; padding: 2px 0; }
.mmc-tip-row span:first-child { color: var(--muted); }

/* survey history table (Survey analytics tab) */
.survey-table-wrap { overflow-x: auto; margin-top: 2px; }
.survey-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; white-space: nowrap; }
.survey-table th, .survey-table td {
  text-align: right; padding: 8px 10px; border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}
.survey-table th { color: var(--muted); font-weight: 600; font-size: 0.78rem; }
.survey-table th:first-child, .survey-table td:first-child { text-align: left; }
.survey-table td.survey-comment-cell {
  text-align: left; white-space: normal; color: var(--muted); min-width: 140px;
}
.survey-table tbody tr:last-child td { border-bottom: none; }
