/* ==========================================================================
   Unerütm – stiilid
   Mobile-first. Vaikimisi rahulik tume teema; hele teema [data-theme="light"].
   ========================================================================== */

:root {
  --bg: #0e1322;
  --surface: #161c2f;
  --surface-2: #1f2742;
  --border: #2e3858;
  --text: #e7eaf3;
  --muted: #aab2c9;
  --accent: #9db8ff;
  --accent-strong: #c6d5ff;
  --accent-contrast: #0e1322;
  --good: #7fd6a8;
  --warn: #f2c46d;
  --danger: #ff9a9a;
  --focus: #ffd479;
  --radius: 14px;
  --shadow: 0 6px 20px rgb(0 0 0 / 0.25);
  --nav-h: 64px;
  --max-w: 760px;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eaeef7;
  --border: #cfd5e4;
  --text: #161b2b;
  --muted: #485169;
  --accent: #2f56c7;
  --accent-strong: #1f3f9c;
  --accent-contrast: #ffffff;
  --good: #1b7449;
  --warn: #8a5a00;
  --danger: #b42323;
  --focus: #b35c00;
  --shadow: 0 4px 14px rgb(20 30 60 / 0.08);
  color-scheme: light;
}

/* ---------- Alus ---------- */
*,
*::before,
*::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 16px);
}

h1, h2, h3 { line-height: 1.25; margin: 0 0 0.6rem; }
h2 { font-size: 1.5rem; margin-top: 0.4rem; }
h2:focus { outline: none; }
h3 { font-size: 1.05rem; color: var(--accent-strong); }
p { margin: 0 0 0.6rem; }
a { color: var(--accent); }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 6px;
}

.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute;
  left: 8px; top: -60px;
  background: var(--accent);
  color: var(--accent-contrast);
  padding: 8px 14px;
  border-radius: 8px;
  z-index: 100;
  text-decoration: none;
  font-weight: 600;
}
.skip-link:focus { top: 8px; }

[hidden] { display: none !important; }

/* ---------- Päis ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { font-size: 1.25rem; margin: 0; letter-spacing: 0.02em; }
.brand-moon { color: var(--accent); }

.icon-btn {
  width: 44px; height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1.2rem;
  cursor: pointer;
}

/* ---------- Navigatsioon (mobiilis all) ---------- */
.app-nav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.app-nav ul {
  list-style: none;
  margin: 0 auto;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  max-width: var(--max-w);
}
.app-nav a {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: var(--nav-h);
  padding: 6px 2px;
  font-size: 0.7rem;
  color: var(--muted);
  text-decoration: none;
}
.app-nav .nav-ico { font-size: 1.25rem; line-height: 1; }
.app-nav a[aria-current="page"] {
  color: var(--accent);
  font-weight: 700;
  box-shadow: inset 0 3px 0 var(--accent);
}

/* ---------- Põhisisu ---------- */
main {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 16px;
}
main:focus { outline: none; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 0.6rem;
}
.card-head h3 { margin: 0; }

.card-hero {
  background: linear-gradient(160deg, var(--surface-2), var(--surface));
}
.big-time {
  font-size: 3rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1.1;
  margin: 0.2rem 0 0.4rem;
  font-variant-numeric: tabular-nums;
}
.big-time small { font-size: 1rem; font-weight: 500; color: var(--muted); }

.hint, .muted { color: var(--muted); font-size: 0.9rem; }
.empty { color: var(--muted); font-style: italic; }

/* ---------- Teated ---------- */
.notice {
  border-radius: 10px;
  padding: 10px 12px;
  margin: 0.6rem 0;
  border: 1px solid var(--border);
  border-left-width: 5px;
  background: var(--surface-2);
}
.notice-ok { border-left-color: var(--good); }
.notice-warn { border-left-color: var(--warn); }
.notice-danger { border-left-color: var(--danger); }
.notice strong { display: block; }

/* ---------- Vormid ---------- */
.form .field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; flex: 1; min-width: 0; }
.field-row { display: flex; gap: 12px; flex-wrap: wrap; }
.field-row > .field { min-width: 130px; }
label, legend { font-weight: 600; font-size: 0.95rem; }

input[type="text"],
input[type="number"],
input[type="date"],
input[type="time"],
select {
  width: 100%;
  min-height: 44px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
input[type="range"] { width: 100%; accent-color: var(--accent); min-height: 32px; }
input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); flex-shrink: 0; margin: 2px 0 0; }
input:invalid:not(:placeholder-shown):not(:focus) { border-color: var(--danger); }

.form-error { color: var(--danger); font-weight: 600; min-height: 0; margin: 0; }
.form-error:empty { display: none; }

.duration-preview { font-size: 1.05rem; }
.duration-preview output { font-weight: 700; color: var(--accent-strong); }

fieldset { border: 0; padding: 0; margin: 0 0 12px; min-width: 0; }

/* Hinnanguskaala 1–5 */
.rating-options { display: flex; gap: 8px; margin: 6px 0 4px; }
.rating-options input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.rating-options label {
  flex: 1;
  min-height: 44px;
  display: grid;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  cursor: pointer;
  font-weight: 700;
}
.rating-options input:checked + label {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}
.rating-options input:focus-visible + label { outline: 3px solid var(--focus); outline-offset: 2px; }

/* Lülitusnupud (7 p / 30 p) */
.segmented { display: inline-flex; margin: 0; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.segmented input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.segmented label { padding: 6px 12px; cursor: pointer; font-weight: 600; min-height: 36px; display: grid; place-items: center; }
.segmented input:checked + label { background: var(--accent); color: var(--accent-contrast); }
.segmented input:focus-visible + label { outline: 3px solid var(--focus); outline-offset: -3px; }

.inline-select select { min-height: 36px; padding: 4px 8px; width: auto; }

/* ---------- Nupud ---------- */
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 6px; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 8px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-small {
  min-height: 36px;
  padding: 4px 10px;
  font-size: 0.85rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-weight: 600;
}
.btn-small.danger { color: var(--danger); }

/* ---------- Loendid ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list > li {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.list > li:last-child { border-bottom: 0; }

.entry-main { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.entry-meta { color: var(--muted); font-size: 0.9rem; }
.entry-actions { display: flex; gap: 8px; margin-top: 6px; }

.task { display: flex; gap: 10px; align-items: flex-start; }
.task-body { flex: 1; min-width: 0; }
.task-title { font-weight: 600; overflow-wrap: anywhere; }
.task.done .task-title { text-decoration: line-through; color: var(--muted); }
.task.overdue .task-meta { color: var(--danger); }
.task-meta { color: var(--muted); font-size: 0.88rem; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 700;
  border: 1px solid currentColor;
  white-space: nowrap;
}
.badge-good { color: var(--good); }
.badge-warn { color: var(--warn); }
.badge-danger { color: var(--danger); }

.stars { color: var(--warn); letter-spacing: 1px; }

/* Päeva koormus */
.load-row { display: grid; grid-template-columns: 5.5rem 1fr auto; gap: 8px; align-items: center; padding: 6px 0; font-size: 0.9rem; }
.load-row meter { width: 100%; height: 12px; }
.load-row .load-label { font-weight: 600; }
.load-row .load-val { color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- Tunniplaan ---------- */
.schedule-grid { display: grid; gap: 12px; grid-template-columns: 1fr; }
.day-card h3 { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.day-card h3 .muted { font-weight: 400; font-size: 0.85rem; }
.lesson { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.lesson-time { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 6.5rem; display: inline-block; }

/* ---------- Statistika ---------- */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 14px; }
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
}
.tile-label { font-size: 0.8rem; color: var(--muted); display: block; }
.tile-value { font-size: 1.4rem; font-weight: 700; display: block; font-variant-numeric: tabular-nums; }
.tile-sub { font-size: 0.8rem; color: var(--muted); }

.chart { width: 100%; height: 220px; display: block; }
.chart-text { margin-top: 8px; font-size: 0.92rem; }
.legend { font-size: 0.8rem; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: center; margin: 8px 0 0; }
.key { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }
.key-good { background: var(--accent); }
.key-warn { background: var(--warn); }
.key-danger { background: var(--danger); }
.key-target { height: 0; border-top: 2px dashed var(--good); border-radius: 0; }

/* Nipid */
.tip { padding: 10px 0; border-bottom: 1px solid var(--border); }
.tip:last-child { border-bottom: 0; }
.tip h4 { margin: 0 0 4px; font-size: 0.98rem; }
.tip p { margin: 0; color: var(--muted); font-size: 0.92rem; }

/* Sünkroonimiskood */
.sync-code {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 1.4rem;
  letter-spacing: 0.12em;
  background: var(--bg);
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  display: inline-block;
  user-select: all;
}
.form-inline { margin-top: 12px; }

/* ---------- Teavitus (toast) ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 14px);
  transform: translate(-50%, 20px);
  max-width: min(92vw, 520px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 10px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-kind="error"] { background: var(--danger); color: #1a0000; }

/* ---------- Suuremad ekraanid ---------- */
@media (min-width: 760px) {
  body { padding-bottom: 24px; }
  .app-nav {
    position: sticky;
    top: 65px;
    bottom: auto;
    border-top: 0;
    border-bottom: 1px solid var(--border);
  }
  .app-nav a { flex-direction: row; font-size: 0.92rem; gap: 6px; min-height: 48px; }
  .app-nav a[aria-current="page"] { box-shadow: inset 0 -3px 0 var(--accent); }
  .tiles { grid-template-columns: repeat(4, 1fr); }
  .schedule-grid { grid-template-columns: 1fr 1fr; }
  .toast { bottom: 24px; }
  .chart { height: 260px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

@media (forced-colors: active) {
  .rating-options input:checked + label,
  .segmented input:checked + label { outline: 2px solid CanvasText; }
}
