:root {
  --bg: #f5f4f8;
  --surface: #ffffff;
  --ink: #1c1b1f;
  --muted: #605d66;
  --line: #e3e0ea;
  --accent: #6750a4;
  --accent-ink: #ffffff;
  --danger: #b3261e;
  --danger-bg: #fbe9e7;
  --warn: #8a5300;
  --warn-bg: #fff1d6;
  --ok: #1e6b3a;
  --ok-bg: #e3f4e8;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
[hidden] { display: none !important; }
h1 { font-size: 1.5rem; margin: 0.25rem 0; }
h2 { font-size: 1.05rem; margin: 0 0 0.25rem; }
p { margin: 0.25rem 0; }
.muted { color: var(--muted); font-size: 0.875rem; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { width: 32px; height: 32px; border-radius: 9px; background: var(--accent); position: relative; }
.brand-mark::after {
  content: ""; position: absolute; inset: 9px; border-radius: 50%;
  border: 3px solid var(--accent-ink);
}
.brand-title { font-weight: 700; }
.brand-sub { color: var(--muted); font-size: 0.8rem; }
.account { display: flex; align-items: center; gap: 12px; font-size: 0.9rem; }

main { max-width: 1400px; margin: 0 auto; padding: 24px 16px 48px; }

.btn {
  border: 1px solid transparent; border-radius: 999px; padding: 8px 18px;
  font: inherit; font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:disabled { opacity: 0.6; cursor: wait; }
.btn.ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn.link { background: none; border: 0; padding: 0; color: var(--accent); font-weight: 600; }
.btn:focus-visible, input:focus-visible, .tile-frame:focus-visible { outline: 3px solid #b9a8ea; outline-offset: 2px; }

.alert {
  background: var(--danger-bg); color: var(--danger); border-radius: var(--radius);
  padding: 12px 16px; margin: 0 0 16px;
}

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 24px; }
.narrow { max-width: 420px; margin: 40px auto; }
form { display: grid; gap: 14px; margin-top: 16px; }
label { display: grid; gap: 6px; font-size: 0.9rem; font-weight: 600; }
input {
  font: inherit; font-weight: 400; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 8px; background: #fff;
}

.section-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }

.exam-list { display: grid; gap: 12px; }
.exam {
  display: flex; justify-content: space-between; align-items: center; gap: 16px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 20px;
}

.live { display: inline-flex; align-items: center; gap: 8px; font-size: 0.9rem; color: var(--muted); }
.live .dot { width: 10px; height: 10px; border-radius: 50%; background: #9e9aa5; }
.live[data-state="live"] { color: var(--ok); }
.live[data-state="live"] .dot { background: #2e9d55; animation: pulse 1.6s infinite; }
.live[data-state="error"], .live[data-state="closed"] { color: var(--danger); }
.live[data-state="error"] .dot, .live[data-state="closed"] .dot { background: var(--danger); }
@keyframes pulse { 50% { opacity: 0.35; } }

.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; display: grid; }
.stat-value { font-size: 1.6rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: 0.8rem; }
.note { color: var(--muted); font-size: 0.8rem; margin-bottom: 16px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.empty { color: var(--muted); text-align: center; padding: 32px 0; }

.tile {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; display: flex; flex-direction: column;
}
.tile.flagged { border-color: var(--danger); box-shadow: 0 0 0 1px var(--danger); }
.tile-head { padding: 8px 10px 6px; }
.tile-name { font-weight: 700; font-size: 0.95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-meta { font-size: 0.78rem; }
.tile-frame {
  position: relative; display: block; width: 100%; aspect-ratio: 1 / 1;
  background: #2b2930; border: 0; padding: 0; cursor: zoom-in;
}
.tile-frame { overflow: hidden; }
.tile-frame img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; }
.tile-placeholder {
  position: absolute; inset: 0; display: grid; place-items: center;
  color: #cfcbd6; font-size: 0.85rem;
}
.tile-age {
  position: absolute; right: 8px; bottom: 8px; font-size: 0.72rem;
  background: rgba(0, 0, 0, 0.55); color: #fff; padding: 2px 8px; border-radius: 999px;
}
.tile-age:empty { display: none; }
.tile.stale .tile-frame img { filter: grayscale(1); opacity: 0.45; }
.tile.stale .tile-age { background: var(--danger); }
.badges { display: flex; flex-wrap: wrap; gap: 4px; padding: 8px 10px 10px; }
.badge { font-size: 0.72rem; padding: 2px 8px; border-radius: 999px; background: #eeebf3; color: var(--ink); }
.badge.danger { background: var(--danger-bg); color: var(--danger); font-weight: 600; }
.badge.warn { background: var(--warn-bg); color: var(--warn); font-weight: 600; }
.badge.ok { background: var(--ok-bg); color: var(--ok); font-weight: 600; }

.viewer {
  position: fixed; inset: 0; background: rgba(20, 18, 24, 0.72);
  display: grid; place-items: center; padding: 16px; z-index: 10;
}
.viewer-box {
  background: var(--surface); border-radius: var(--radius); padding: 16px;
  width: min(560px, 100%); max-height: 100%; display: grid; gap: 12px;
}
.viewer-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.viewer-name { font-weight: 700; font-size: 1.1rem; }
.viewer-box img {
  width: 100%; max-height: 70vh; object-fit: contain; background: #2b2930; border-radius: 8px;
  min-height: 200px;
}

@media (max-width: 640px) {
  .topbar { padding: 10px 16px; }
  .brand-sub, #teacher-name { display: none; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .exam { flex-direction: column; align-items: stretch; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
}
