:root {
  --bg: #ffffff;
  --surface: #f5f5f7;
  --elevated: #fbfbfd;
  --text: #1d1d1f;
  --muted: #86868b;
  --border: rgba(0, 0, 0, 0.06);
  --border-strong: rgba(0, 0, 0, 0.1);
  --accent: #0071e3;
  --accent-hover: #0077ed;
  --accent-soft: rgba(0, 113, 227, 0.1);
  --danger: #ff3b30;
  --slot-hover: rgba(0, 0, 0, 0.04);
  --fill: rgba(0, 0, 0, 0.05);
  --nav-bg: rgba(255, 255, 255, 0.72);
  --seg-active: #ffffff;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 18px;
  --ease: cubic-bezier(0.25, 0.1, 0.25, 1);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Helvetica Neue", "Segoe UI", Helvetica, Arial, sans-serif;
  color-scheme: light;
}

/* Dark: follows the system unless the user picked Light, or forced via the switch */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000000;
    --surface: #1c1c1e;
    --elevated: #2c2c2e;
    --text: #f5f5f7;
    --muted: #a1a1a6;
    --border: rgba(255, 255, 255, 0.08);
    --border-strong: rgba(255, 255, 255, 0.14);
    --accent: #2997ff;
    --accent-hover: #47a6ff;
    --accent-soft: rgba(41, 151, 255, 0.16);
    --danger: #ff453a;
    --slot-hover: rgba(255, 255, 255, 0.05);
    --fill: rgba(255, 255, 255, 0.08);
    --nav-bg: rgba(22, 22, 23, 0.72);
    --seg-active: #636366;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #000000;
  --surface: #1c1c1e;
  --elevated: #2c2c2e;
  --text: #f5f5f7;
  --muted: #a1a1a6;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);
  --accent: #2997ff;
  --accent-hover: #47a6ff;
  --accent-soft: rgba(41, 151, 255, 0.16);
  --danger: #ff453a;
  --slot-hover: rgba(255, 255, 255, 0.05);
  --fill: rgba(255, 255, 255, 0.08);
  --nav-bg: rgba(22, 22, 23, 0.72);
  --seg-active: #636366;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

/* Smooth cross-fade when the theme changes */
body, .card, .topbar-wrap, .tt-head, input, select {
  transition: background-color 0.4s var(--ease), color 0.3s var(--ease),
              border-color 0.4s var(--ease), box-shadow 0.4s var(--ease);
}

* { box-sizing: border-box; }

/* Never scroll sideways */
html, body { overflow-x: clip; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.47 var(--font);
  letter-spacing: -0.016em;
  -webkit-font-smoothing: antialiased;
}

h1 { font-size: 1.15rem; font-weight: 600; letter-spacing: -0.01em; margin: 0; }
h2 {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 0 0 12px;
}

button, .btn, input, select {
  font: inherit;
  color: inherit;
  letter-spacing: inherit;
}

:focus-visible { outline: 3px solid var(--accent-soft); outline-offset: 1px; }
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* Buttons */
button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--fill);
  border: none;
  border-radius: 980px;
  padding: 7px 16px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s var(--ease), transform 0.15s var(--ease), color 0.2s var(--ease);
}
button:hover, .btn:hover { background: var(--border-strong); }
button:active, .btn:active { transform: scale(0.97); }
button[type="submit"] { background: var(--accent); color: #fff; }
button[type="submit"]:hover { background: var(--accent-hover); }
button.small { padding: 5px 12px; font-size: 0.78rem; }
.danger { color: var(--danger); }
[hidden] { display: none !important; }

/* Fields */
input, select {
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 7px 10px;
  min-width: 0;
  max-width: 100%;
  transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}
input::placeholder { color: var(--muted); }
input[type="color"] {
  flex: none;
  padding: 3px;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
input[type="color"]::-webkit-color-swatch { border: none; border-radius: 5px; }

/* Top bar: frosted, sticky */
.topbar-wrap {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--nav-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border);
}
.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  align-items: center;
  justify-content: space-between;
  min-height: 52px;
  padding: 8px 24px;
  max-width: 1440px;
  margin: 0 auto;
}
.topbar-right { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.actions button, .actions .btn { padding: 6px 14px; }

/* Segmented controls (appearance, layout) */
.segmented {
  display: inline-flex;
  padding: 2px;
  gap: 2px;
  background: var(--fill);
  border-radius: 980px;
}
.segmented button {
  background: transparent;
  padding: 5px 14px;
  font-size: 0.8rem;
  color: var(--muted);
}
.segmented button:hover { background: transparent; color: var(--text); }
.segmented button[aria-checked="true"] {
  background: var(--seg-active);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

/* Page intro: kept short so the timetable sits near the top */
.intro {
  max-width: 1440px;
  margin: 0 auto;
  padding: 32px 24px 24px;
}
.intro h2 {
  font-size: clamp(1.6rem, 3.5vw, 2.25rem);
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.015em;
  text-transform: none;
  color: var(--text);
  margin: 0 0 6px;
}
.intro p { font-size: 1rem; color: var(--muted); margin: 0; max-width: 40em; }

/* Layout: tools on the left, timetable plus the smaller cards on the right */
.layout {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  gap: 20px;
  padding: 0 24px 48px;
  max-width: 1440px;
  margin: 0 auto;
  align-items: start;
}
.sidebar, .main-col { display: flex; flex-direction: column; gap: 20px; min-width: 0; }
.sub-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 20px;
  align-items: start;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 20px;
  min-width: 0;
}

.hint { color: var(--muted); font-size: 0.8rem; line-height: 1.45; margin: 0 0 12px; }

.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.card-head h2 { margin: 0; }

.dash { color: var(--muted); }

/* Subjects: compact pills that wrap */
.subject-list { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.subject {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  border-radius: 980px;
  background: var(--fill);
  transition: background-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}
.subject.active { background: var(--accent-soft); box-shadow: inset 0 0 0 1.5px var(--accent); }
.subject.active .pick { color: var(--accent); }
.subject .pick {
  min-width: 0;
  gap: 7px;
  background: transparent;
  padding: 6px 4px 6px 10px;
  font-size: 0.84rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.subject .pick:only-child { padding-right: 12px; }
.subject .pick:hover { background: transparent; }
.swatch { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.swatch.erase { border: 1.5px dashed var(--muted); }
.icon {
  flex: none;
  background: transparent;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
  width: 24px;
  height: 24px;
  padding: 0;
  margin-right: 3px;
  opacity: 0.6;
}
.icon:hover { color: var(--danger); background: transparent; opacity: 1; }

.inline-form { display: flex; align-items: center; gap: 8px; }
.inline-form input:first-child { flex: 1 1 0; width: 0; }
.stack-form { display: flex; flex-direction: column; gap: 8px; }
.stack-form button { padding: 9px 16px; margin-top: 2px; }

/* Periods editor */
.period-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.period-row {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto minmax(0, 1fr) 24px;
  align-items: center;
  gap: 4px;
}
.period-row input { width: 100%; padding: 5px 6px; font-size: 0.82rem; }
.period-row .p-label { font-weight: 600; }
.period-row .icon { margin-right: 0; }
.period-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* Summary */
.summary { display: flex; flex-direction: column; gap: 12px; font-size: 0.88rem; }
.bar-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 8px; }
.bar-row > span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-row > span:nth-child(2) { color: var(--muted); font-variant-numeric: tabular-nums; }
.bar { grid-column: 1 / -1; height: 5px; background: var(--fill); border-radius: 980px; overflow: hidden; }
.bar span { display: block; height: 100%; border-radius: 980px; transition: width 0.5s var(--ease); }
.total {
  color: var(--text);
  font-weight: 600;
  border-top: 1px solid var(--border-strong);
  padding-top: 12px;
}

/* Exams */
.exam-list { list-style: none; padding: 0; margin: 0 0 14px; display: flex; flex-direction: column; gap: 12px; }
.exam-list li { display: flex; align-items: center; gap: 10px; font-size: 0.88rem; }
.exam-list .info { flex: 1; min-width: 0; font-weight: 500; }
.exam-list .info > div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.exam-list .date { color: var(--muted); font-size: 0.76rem; font-weight: 400; }
.countdown {
  font-weight: 600;
  font-size: 0.74rem;
  white-space: nowrap;
  padding: 3px 9px;
  border-radius: 980px;
  background: var(--fill);
  font-variant-numeric: tabular-nums;
}
.countdown.soon { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
.countdown.past { color: var(--muted); font-weight: 500; }

.settings { display: flex; flex-wrap: wrap; gap: 12px; }
.settings label { display: flex; align-items: center; gap: 8px; font-size: 0.88rem; color: var(--muted); }
.settings select { color: var(--text); }

/* Timetable card */
.grid-card { padding: 0; overflow: hidden; background: var(--elevated); }
.grid-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.toolbar-label { font-size: 0.75rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.grid { user-select: none; }
.grid-empty { padding: 48px 24px; margin: 0; text-align: center; }

/* Time-scaled timetable, so breaks show as gaps. Columns shrink to fit: no sideways scrolling */
.tt-head, .tt-body {
  display: grid;
  grid-template-columns: 72px repeat(7, minmax(0, 1fr));
}
.tt-head {
  background: var(--elevated);
  border-bottom: 1px solid var(--border-strong);
}
.tt-head > div {
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.tt-body { box-sizing: content-box; padding: 8px 0 10px; }
.tt-axis, .tt-day { position: relative; min-width: 0; }
.tt-day { border-left: 1px solid var(--border); }
.tt-time {
  position: absolute;
  right: 8px;
  font-size: 0.68rem;
  line-height: 1.3;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.tt-time b { color: var(--text); font-weight: 600; }

.slot {
  position: absolute;
  border-radius: var(--radius-sm);
  font-size: 0.76rem;
  font-weight: 600;
  overflow: hidden;
}
.slot {
  left: 3px;
  right: 3px;
  display: flex;
  align-items: center;
  padding: 0 8px;
  white-space: nowrap;
  text-overflow: ellipsis;
  background: var(--slot-hover);
  cursor: pointer;
  transition: background-color 0.15s var(--ease);
}
.slot:not(.filled):hover { background: var(--fill); }
.slot.filled { box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.18); }

/* Medium screens: sidebar tools move under the timetable */
@media (max-width: 1100px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); align-items: start; order: 1; }
}

/* Phones: everything in one column, tighter spacing, compact grid */
@media (max-width: 640px) {
  .topbar-wrap { position: static; }
  .topbar { padding: 8px 16px; }
  .intro { padding: 20px 16px 16px; }
  .intro p { display: none; }
  .layout { padding: 0 12px 32px; gap: 12px; }
  .sidebar, .main-col, .sub-cards { gap: 12px; }
  .card { padding: 16px; }
  .grid-toolbar { padding: 10px 12px; }

  .tt-head, .tt-body { grid-template-columns: 34px repeat(7, minmax(0, 1fr)); }
  .tt-head > div { font-size: 0.62rem; letter-spacing: 0.02em; min-height: 34px; }
  .tt-time { right: 4px; font-size: 0.6rem; }
  .tt-time b + .t { display: none; }
  .slot { left: 1px; right: 1px; padding: 0 3px; font-size: 0.62rem; border-radius: 5px; text-overflow: clip; }
}

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

@media print {
  body { background: #fff; color: #000; }
  .topbar-wrap { position: static; background: none; backdrop-filter: none; border: none; }
  .topbar-right, .sidebar, .sub-cards, .intro { display: none; }
  .layout { display: block; padding: 0; }
  .card { border: none; box-shadow: none; background: #fff; }
  .grid-toolbar { display: none; }
  .slot { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
