:root {
  --bg: #f6f5f1;
  --card: #ffffff;
  --ink: #1d2433;
  --muted: #667085;
  --line: #e3e1da;
  --accent: #1f3a5f;
  --accent-soft: #e6edf6;
  --danger: #b42318;
  --mark: #fff1a8;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171d; --card: #1c2028; --ink: #e8eaf0; --muted: #98a2b3; --line: #2c313c;
    --accent: #8db4e8; --accent-soft: #233149; --danger: #f97066; --mark: #5c4d00;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; gap: 12px; align-items: center;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { display: flex; gap: 8px; align-items: center; text-decoration: none; color: var(--ink); font-weight: 700; white-space: nowrap; }
.search { flex: 1; max-width: 560px; }
.search input {
  width: 100%; padding: 9px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--bg); font-size: 16px; /* 16px stops iPhone zooming in */
}
.icon-btn { border: 0; background: none; font-size: 22px; padding: 6px 10px; cursor: pointer; border-radius: 8px; margin-left: auto; }
.icon-btn:hover { background: var(--accent-soft); }
.menu {
  position: absolute; right: 12px; top: 100%; margin-top: 4px; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); min-width: 220px; padding: 6px; display: grid;
}
.menu a, .menu button { text-align: left; padding: 10px 12px; border: 0; background: none; border-radius: 8px; text-decoration: none; color: var(--ink); cursor: pointer; }
.menu a:hover, .menu button:hover { background: var(--accent-soft); }
.menu-user { padding: 8px 12px; color: var(--muted); font-size: 14px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
@media (max-width: 560px) {
  .topbar { flex-wrap: wrap; }
  .brand span { display: none; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
}

main { max-width: 1100px; margin: 0 auto; padding: 20px 16px 120px; outline: none; }
h1 { font-size: 1.6rem; margin: 0 0 4px; line-height: 1.25; word-break: break-word; }
h2 { font-size: 1.1rem; margin: 28px 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 9px 16px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card); cursor: pointer; text-decoration: none; color: var(--ink);
  min-height: 42px; user-select: none;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--card); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 4px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 18px; }

/* Breadcrumbs */
.crumbs { display: flex; flex-wrap: wrap; gap: 4px; font-size: 14px; color: var(--muted); margin-bottom: 6px; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { color: var(--accent); text-decoration: underline; }
.crumbs span.sep { opacity: .5; }

/* Subject cards */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px;
  text-decoration: none; color: var(--ink); box-shadow: var(--shadow); display: block;
}
.card:hover { border-color: var(--accent); }
.card .title { font-weight: 650; font-size: 1.05rem; word-break: break-word; }
.card .meta { color: var(--muted); font-size: 14px; margin-top: 4px; }
.card .dot { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; font-weight: 700; margin-bottom: 10px; color: #fff; }

/* Lists */
.list { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 11px 14px; border-top: 1px solid var(--line); min-height: 56px; }
.row:first-child { border-top: 0; }
.row .ic { width: 36px; height: 36px; flex: none; border-radius: 8px; display: grid; place-items: center; font-size: 11px; font-weight: 700; background: var(--accent-soft); color: var(--accent); text-transform: uppercase; }
.row .main { flex: 1; min-width: 0; }
.row .name { display: block; text-decoration: none; color: var(--ink); font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
a.row:hover, .row a.name:hover { color: var(--accent); }
.row .sub { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .snip { color: var(--muted); font-size: 14px; margin-top: 2px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.row .actions { display: flex; gap: 2px; flex: none; }
.row .actions button, .row .actions a { border: 0; background: none; cursor: pointer; padding: 8px; border-radius: 8px; color: var(--muted); text-decoration: none; font-size: 14px; }
.row .actions button:hover, .row .actions a:hover { background: var(--accent-soft); color: var(--accent); }
a.row { text-decoration: none; color: inherit; }
mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 1px; }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); border: 2px dashed var(--line); border-radius: var(--radius); }

/* Auth */
.auth { max-width: 400px; margin: 8vh auto 0; background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); }
.auth .logo { display: flex; gap: 10px; align-items: center; font-weight: 700; font-size: 1.3rem; margin-bottom: 16px; }
label { display: block; font-size: 14px; font-weight: 550; margin: 14px 0 6px; }
input[type=text], input[type=email], input[type=password], textarea, select {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-size: 16px;
}
textarea { min-height: 90px; resize: vertical; }
.error { color: var(--danger); font-size: 14px; margin-top: 10px; }
.auth .btn.primary { width: 100%; justify-content: center; margin-top: 18px; }

/* Viewer */
.viewer { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.viewer img, .viewer video { display: block; max-width: 100%; margin: 0 auto; }
.viewer pre { margin: 0; padding: 16px; white-space: pre-wrap; word-break: break-word; font: 14px/1.5 ui-monospace, Consolas, monospace; max-height: 75vh; overflow: auto; }
.viewer audio { width: 100%; padding: 16px; }
.pdf-pages { background: #525659; padding: 8px; display: grid; gap: 8px; justify-items: center; max-height: 80vh; overflow: auto; }
.pdf-pages canvas { max-width: 100%; height: auto !important; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.viewer .note { padding: 24px; text-align: center; color: var(--muted); }

/* Zip tree */
.zip-path { font-size: 14px; color: var(--muted); margin: 0 0 8px; display: flex; flex-wrap: wrap; gap: 4px; }

/* Uploads panel */
.uploads {
  position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); width: min(380px, calc(100vw - 32px));
  max-height: 50vh; overflow: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 8px 30px rgba(0,0,0,.18); z-index: 30;
}
.uploads header { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--card); }
.uploads ul { list-style: none; margin: 0; padding: 0; }
.uploads li { padding: 8px 14px; font-size: 14px; border-top: 1px solid var(--line); }
.uploads li:first-child { border-top: 0; }
.uploads .bar { height: 4px; background: var(--line); border-radius: 4px; margin-top: 4px; overflow: hidden; }
.uploads .bar > div { height: 100%; background: var(--accent); width: 0; transition: width .2s; }
.uploads li.done .bar > div { background: #12b76a; }
.uploads li.err { color: var(--danger); }
.uploads .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.drop-overlay { position: fixed; inset: 0; background: rgba(31, 58, 95, .82); z-index: 40; display: grid; place-items: center; color: #fff; font-size: 1.3rem; font-weight: 600; pointer-events: none; }
.drop-overlay div { border: 3px dashed #fff; border-radius: 20px; padding: 40px; margin: 20px; text-align: center; }

/* Dialog */
dialog { border: 1px solid var(--line); border-radius: 16px; padding: 0; width: min(640px, calc(100vw - 24px)); background: var(--card); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.dlg { padding: 20px; }
.dlg h3 { margin: 0 0 8px; }
.dlg .buttons { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
.ai-out { margin-top: 14px; padding: 14px; border-radius: 10px; background: var(--bg); max-height: 50vh; overflow: auto; font-size: 15px; }
.ai-out h1, .ai-out h2, .ai-out h3 { font-size: 1.05rem; margin: 12px 0 6px; }
.ai-out p { margin: 6px 0; }
.ai-out ul, .ai-out ol { padding-left: 22px; margin: 6px 0; }
.ai-out code { background: var(--accent-soft); padding: 0 4px; border-radius: 4px; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 10px 0; }
.tabs button { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 14px; cursor: pointer; }
.tabs button[aria-pressed=true] { background: var(--accent); color: var(--card); border-color: var(--accent); }
.notice { background: var(--accent-soft); border-radius: 10px; padding: 12px 14px; font-size: 14px; }

.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(24px + env(safe-area-inset-bottom)); background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 999px; z-index: 50; font-size: 14px; max-width: calc(100vw - 32px); }
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -3px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } .uploads .bar > div { transition: none; } }
.code-box { font-family: ui-monospace, Consolas, monospace; background: var(--bg); padding: 10px 12px; border-radius: 10px; word-break: break-all; font-size: 14px; }
