:root {
  --bg: #ffffff;
  --card: #f1f1f3;
  --card-active: #e5e5e8;
  --text: #1b1b1f;
  --muted: #6b6b74;
  --faint: #9b9ba3;
  --line: #e7e7ea;
  --accent: #dc143c;
  --accent-text: #c8102e;
  --accent-soft: rgba(220, 20, 60, 0.1);
  --on-accent: #ffffff;
  --sk-1: #e8e8eb;
  --sk-2: #f6f6f7;
  --shadow: 0 10px 28px rgba(0, 0, 0, 0.14);
  --max: 560px;
  color-scheme: light;
}

html[data-theme="dark"] {
  --bg: #1f1f21;
  --card: #2c2c2f;
  --card-active: #37373b;
  --text: #ececef;
  --muted: #a3a3ab;
  --faint: #7c7c84;
  --line: #333337;
  --accent-text: #ff5a76;
  --accent-soft: rgba(220, 20, 60, 0.2);
  --sk-1: #2a2a2d;
  --sk-2: #3a3a3e;
  --shadow: 0 10px 28px rgba(0, 0, 0, 0.55);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  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.45;
  -webkit-tap-highlight-color: transparent;
}

a { color: inherit; }

button { font: inherit; cursor: pointer; }

svg { display: block; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* Press feedback: buttons and cards shrink slightly. */
.pressable {
  transition: transform 0.12s ease, background-color 0.12s ease;
  -webkit-user-select: none;
  user-select: none;
}
.pressable:active { transform: scale(0.96); }

/* ---------- Layout ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 4px;
  max-width: var(--max);
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 8px) 8px 8px 16px;
  background: var(--bg);
}
.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar .icon-btn:first-child { margin-left: -10px; }

.view {
  max-width: var(--max);
  margin: 0 auto;
  padding: 4px 16px calc(92px + env(safe-area-inset-bottom));
}
.view.from-right { animation: in-right 0.22s ease-out; }
.view.from-left { animation: in-left 0.22s ease-out; }
@keyframes in-right { from { opacity: 0.4; transform: translateX(28px); } to { opacity: 1; transform: none; } }
@keyframes in-left { from { opacity: 0.4; transform: translateX(-28px); } to { opacity: 1; transform: none; } }

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  max-width: var(--max);
  margin: 0 auto;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--line);
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 0;
  border-radius: 14px;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.75rem;
  font-weight: 500;
}
.tab svg { width: 24px; height: 24px; color: var(--muted); }
.tab[aria-current="page"] {
  color: var(--accent-text);
  font-weight: 700;
  background: var(--accent-soft);
}
.tab[aria-current="page"] svg { color: var(--accent); }

/* ---------- Building blocks ---------- */

.icon-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--accent);
  text-decoration: none;
}
.icon-btn svg { width: 24px; height: 24px; }

.download {
  width: 40px;
  height: 40px;
  background: var(--accent);
  color: var(--on-accent);
}
.download svg { width: 22px; height: 22px; }

.card {
  background: var(--card);
  border-radius: 16px;
  padding: 14px 16px;
}

.badge-new {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 1.5;
}

.tag-lab {
  flex: none;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.section-title {
  margin: 18px 0 10px;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--muted);
}
.section-title:first-child { margin-top: 6px; }

.message {
  padding: 32px 16px;
  text-align: center;
  color: var(--muted);
}
.message .btn-retry {
  margin-top: 12px;
  padding: 10px 18px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}

/* ---------- Lists (file-manager style) ---------- */

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.row,
.file-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  padding: 10px 10px 10px 12px;
  border-radius: 14px;
  background: var(--card);
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
}
.row:active,
.file-row:active { background: var(--card-active); }

.row-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent);
}
.row-icon svg { width: 24px; height: 24px; }
.row-main { flex: 1; min-width: 0; }
.row-title { display: block; font-weight: 600; }
.row-meta { display: block; font-size: 0.8rem; color: var(--muted); }
.row-chevron { flex: none; color: var(--faint); }
.row-chevron svg { width: 20px; height: 20px; }

.file-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: 0.64rem;
  font-weight: 800;
  letter-spacing: 0.03em;
}
.file-main { flex: 1; min-width: 0; }
.file-name {
  font-weight: 600;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}
.file-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 6px;
  margin-top: 2px;
  font-size: 0.8rem;
  color: var(--muted);
}

/* ---------- Subject grid ---------- */

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.tile {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  min-height: 100px;
  padding: 16px;
  border-radius: 16px;
  background: var(--card);
  color: var(--text);
  text-decoration: none;
}
.tile:active { background: var(--card-active); }
.tile svg { width: 30px; height: 30px; color: var(--accent); }
.tile-name { font-size: 1.05rem; font-weight: 700; }

/* ---------- Sort control ---------- */

.sort {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
  font-size: 0.85rem;
  color: var(--muted);
}
.segmented {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  border-radius: 12px;
  background: var(--card);
}
.segmented button {
  padding: 7px 14px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
}
.segmented button[aria-pressed="true"] {
  background: var(--accent);
  color: var(--on-accent);
}

/* ---------- Updates feed ---------- */

.feed { display: flex; flex-direction: column; gap: 10px; }
.update-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  font-size: 0.8rem;
  color: var(--muted);
}
.update-text {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* ---------- Home: search ---------- */

.search { position: relative; z-index: 5; }
.search-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 48px;
  padding: 0 14px;
  border-radius: 14px;
  background: var(--card);
}
.search-bar:focus-within { box-shadow: inset 0 0 0 2px var(--accent); }
.search-bar svg { flex: none; width: 20px; height: 20px; color: var(--accent); }
.search-bar input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  font: inherit;
}
.search-bar input::placeholder { color: var(--faint); }
.search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  max-height: 60vh;
  overflow-y: auto;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--bg);
  box-shadow: var(--shadow);
}
.search-results .section-title { margin: 6px 4px 8px; font-size: 0.8rem; }
.search-results .message { padding: 20px 12px; }

/* ---------- Home: timetable ---------- */

.timetable {
  margin-top: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
}
.tt {
  width: 100%;
  border-collapse: collapse;
  border-style: hidden; /* outer edge comes from .timetable */
  table-layout: fixed;
}
.tt-time-col { width: 46px; }
.tt th,
.tt td {
  padding: 7px 2px;
  border: 1px solid var(--line);
  text-align: center;
  vertical-align: middle;
  overflow-wrap: break-word;
}
.tt thead th {
  padding: 8px 2px;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--muted);
}
.tt thead th.today { background: var(--accent); color: var(--on-accent); }
.tt td.today { background: var(--accent-soft); }
.tt-time {
  font-size: 0.66rem;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
  color: var(--faint);
}
.tt-time span { display: block; }
.tt-time span:first-child { color: var(--muted); font-weight: 600; }
.tt-class + .tt-class { margin-top: 5px; }
.tt-sub { display: block; font-size: 0.8rem; font-weight: 700; line-height: 1.2; }
.tt-teach { display: block; margin-top: 1px; font-size: 0.6rem; line-height: 1.2; color: var(--faint); }
.tt .tag-lab { display: inline-block; margin-top: 5px; padding: 1px 5px; font-size: 0.56rem; }
.tt-pause th,
.tt-pause td { padding-top: 4px; padding-bottom: 4px; }
.tt-pause td { font-size: 0.75rem; font-style: italic; color: var(--muted); letter-spacing: 0.02em; }
.tt-free { color: var(--faint); font-size: 0.8rem; }

/* ---------- Shimmer placeholders ---------- */

.skeleton {
  border-radius: 8px;
  background: linear-gradient(90deg, var(--sk-1) 25%, var(--sk-2) 37%, var(--sk-1) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.4s ease infinite;
}
@keyframes shimmer {
  from { background-position: 100% 50%; }
  to { background-position: 0 50%; }
}
.sk-row { display: flex; align-items: center; gap: 12px; padding: 10px 12px; min-height: 64px; }
.sk-icon { flex: none; width: 44px; height: 44px; border-radius: 12px; }
.sk-lines { flex: 1; display: flex; flex-direction: column; gap: 8px; }
.sk-line { height: 12px; }
.sk-card { height: 88px; border-radius: 16px; }

@media (prefers-reduced-motion: reduce) {
  .view.from-right,
  .view.from-left { animation: none; }
  .pressable { transition: none; }
}
