:root {
  color-scheme: light dark;
  --bg: #F2F2F7;
  --surface: #FFFFFF;
  --surface-2: #F2F2F7;
  --fill: rgba(118, 118, 128, 0.12);
  --label: #000000;
  --label-2: #6C6C70;
  --separator: rgba(60, 60, 67, 0.18);
  --accent: #0066CC;
  --accent-soft: rgba(0, 102, 204, 0.12);
  --red: #D70015;
  --red-soft: rgba(215, 0, 21, 0.1);
  --orange: #C93400;
  --orange-soft: rgba(201, 52, 0, 0.1);
  --green: #248A3D;
  --green-soft: rgba(36, 138, 61, 0.12);
  --glass: rgba(255, 255, 255, 0.72);
  --glass-border: rgba(255, 255, 255, 0.6);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Hebrew", system-ui, "Segoe UI", "Heebo", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1C1C1E;
    --surface-2: #2C2C2E;
    --fill: rgba(118, 118, 128, 0.24);
    --label: #FFFFFF;
    --label-2: #AEAEB2;
    --separator: rgba(84, 84, 88, 0.6);
    --accent: #4BA3FF;
    --accent-soft: rgba(75, 163, 255, 0.16);
    --red: #FF6961;
    --red-soft: rgba(255, 105, 97, 0.16);
    --orange: #FFB340;
    --orange-soft: rgba(255, 179, 64, 0.16);
    --green: #30DB5B;
    --green-soft: rgba(48, 219, 91, 0.16);
    --glass: rgba(30, 30, 32, 0.72);
    --glass-border: rgba(255, 255, 255, 0.08);
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--label);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 22px; height: 22px; flex-shrink: 0; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 8px; }

/* ---------- Layout ---------- */
.app { min-height: 100dvh; }
.sidebar { display: none; }
.main {
  max-width: 760px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) max(16px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 110px) max(16px, env(safe-area-inset-left));
}

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0 12px;
}
.page-head h1 {
  margin: 0;
  font-size: 34px;
  line-height: 1.15;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.btn-round {
  width: 44px; height: 44px;
  border-radius: 50%;
  border: 0;
  display: grid; place-items: center;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 4px 14px var(--accent-soft);
  transition: transform .15s ease;
}
.btn-round:active { transform: scale(.94); }

.controls { display: grid; gap: 12px; margin-bottom: 18px; }
.search {
  display: flex; align-items: center; gap: 8px;
  background: var(--fill);
  border-radius: 12px;
  padding: 0 12px;
  height: 40px;
  color: var(--label-2);
}
.search svg { width: 18px; height: 18px; }
.search input {
  flex: 1; border: 0; background: none; outline: none; min-width: 0;
  color: var(--label); font-size: 17px; height: 100%;
}
.search input::placeholder { color: var(--label-2); }
.search:focus-within { outline: 3px solid var(--accent-soft); }

.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  background: var(--fill);
  border-radius: 10px;
  padding: 2px;
}
.segmented button {
  border: 0; background: none;
  height: 32px; border-radius: 8px;
  font-size: 15px; font-weight: 500;
  color: var(--label);
}
.segmented button[aria-selected="true"] {
  background: var(--surface);
  box-shadow: 0 2px 6px rgba(0,0,0,.12);
  font-weight: 600;
}
@media (prefers-color-scheme: dark) {
  .segmented button[aria-selected="true"] { background: #636366; }
}

/* ---------- List ---------- */
.list { display: grid; gap: 22px; }
.group-title {
  margin: 0 4px 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--label-2);
}
.group { display: grid; gap: 10px; }

.card {
  display: flex;
  align-items: stretch;
  gap: 14px;
  width: 100%;
  text-align: start;
  background: var(--surface);
  border: 0;
  border-radius: var(--radius);
  padding: 14px;
  transition: transform .15s ease, background .15s;
}
.card:active { transform: scale(.985); }
@media (hover: hover) { .card:hover { background: color-mix(in srgb, var(--surface) 92%, var(--accent)); } }

/* signature: days-left block */
.days {
  width: 62px;
  flex-shrink: 0;
  border-radius: 11px;
  display: grid;
  place-content: center;
  text-align: center;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 6px 0;
}
.days b { display: block; font-size: 26px; line-height: 1; font-weight: 700; font-variant-numeric: tabular-nums; }
.days small { display: block; font-size: 12px; font-weight: 600; margin-top: 3px; }
.days.soon { background: var(--red-soft); color: var(--red); }
.days.week { background: var(--orange-soft); color: var(--orange); }
.days.past { background: var(--fill); color: var(--label-2); }
.days.word b { font-size: 18px; }

.card-body { flex: 1; min-width: 0; display: grid; gap: 4px; align-content: center; }
.card-title { font-size: 17px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-sub { font-size: 15px; color: var(--label-2); display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: center; }
.card-sub .meta { display: inline-flex; align-items: center; gap: 3px; }
.card-sub svg { width: 15px; height: 15px; }
.subject {
  display: inline-block;
  font-size: 13px; font-weight: 600;
  color: var(--accent);
}

.progress {
  height: 4px; border-radius: 4px;
  background: var(--fill);
  overflow: hidden;
  margin-top: 4px;
}
.progress i { display: block; height: 100%; background: var(--green); border-radius: 4px; }

.check {
  width: 28px; height: 28px; border-radius: 50%;
  border: 2px solid var(--label-2);
  background: none;
  display: grid; place-items: center;
  color: transparent;
  flex-shrink: 0;
  align-self: center;
  position: relative;
}
.check::after { content: ""; position: absolute; inset: -8px; } /* 44px hit area */
.check svg { width: 16px; height: 16px; }
.check[aria-checked="true"] { background: var(--green); border-color: var(--green); color: #fff; }
.card.is-done .card-title { color: var(--label-2); text-decoration: line-through; }

.empty {
  text-align: center;
  padding: 56px 20px;
  color: var(--label-2);
}
.empty svg { width: 52px; height: 52px; color: var(--accent); opacity: .5; margin-bottom: 10px; }
.empty h2 { margin: 0 0 6px; font-size: 20px; color: var(--label); }
.empty p { margin: 0 0 18px; }

.btn {
  height: 44px;
  padding: 0 20px;
  border-radius: 12px;
  border: 0;
  font-weight: 600;
  font-size: 17px;
  background: var(--fill);
  color: var(--accent);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { color: var(--red); }
.btn:disabled { opacity: .5; cursor: default; }

/* ---------- Tab bar (mobile) ---------- */
.tabbar {
  position: fixed;
  inset-inline: 0;
  bottom: calc(env(safe-area-inset-bottom) + 10px);
  margin: 0 auto;
  width: min(320px, calc(100% - 32px));
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 5px;
  border-radius: 999px;
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(24px) saturate(180%);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  box-shadow: var(--shadow);
  z-index: 10;
}
.tab {
  border: 0; background: none;
  height: 54px;
  border-radius: 999px;
  display: grid; justify-items: center; align-content: center; gap: 2px;
  font-size: 11px; font-weight: 600;
  color: var(--label-2);
}
.tab svg { width: 24px; height: 24px; }
.tab[aria-current="page"] { color: var(--accent); background: var(--accent-soft); }

@media (prefers-reduced-transparency: reduce) {
  .tabbar { background: var(--surface); backdrop-filter: none; -webkit-backdrop-filter: none; }
}

/* ---------- Desktop ---------- */
@media (min-width: 900px) {
  .app { display: grid; grid-template-columns: 260px 1fr; }
  .tabbar { display: none; }
  .sidebar {
    display: flex; flex-direction: column;
    position: sticky; top: 0; height: 100dvh;
    padding: 20px 12px;
    background: var(--surface);
    border-inline-end: 1px solid var(--separator);
  }
  .brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 20px; padding: 4px 10px 22px; }
  .brand img { border-radius: 8px; }
  .side-nav { display: grid; gap: 2px; }
  .side-item {
    display: flex; align-items: center; gap: 10px;
    border: 0; background: none;
    height: 40px; padding: 0 10px;
    border-radius: 9px;
    font-size: 15px; font-weight: 500;
    text-align: start;
  }
  .side-item svg { color: var(--accent); width: 20px; height: 20px; }
  .side-item span:nth-child(2) { flex: 1; }
  .side-item .count { font-size: 13px; color: var(--label-2); font-variant-numeric: tabular-nums; }
  .side-item:hover { background: var(--fill); }
  .side-item[aria-current="page"] { background: var(--accent); color: #fff; }
  .side-item[aria-current="page"] svg, .side-item[aria-current="page"] .count { color: #fff; }
  .sync { margin-top: auto; padding: 0 10px; font-size: 13px; color: var(--label-2); }
  .main { padding: 28px 40px 60px; max-width: 980px; margin: 0; width: 100%; }
  .controls { grid-template-columns: 1fr 280px; }
  .group { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
  body { font-size: 15px; }
}

/* ---------- Sheet ---------- */
.sheet {
  border: 0;
  padding: 0;
  color: var(--label);
  background: var(--surface-2);
  width: 100%;
  max-width: 100%;
  max-height: 92dvh;
  margin: auto 0 0;
  border-radius: 14px 14px 0 0;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.sheet[open] { display: flex; flex-direction: column; animation: sheet-up .32s cubic-bezier(.2,.9,.3,1); }
.sheet::backdrop { background: rgba(0,0,0,.35); animation: fade .25s ease; }
@media (prefers-color-scheme: dark) { .sheet { background: #1C1C1E; } }
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }
@media (prefers-reduced-motion: reduce) {
  .sheet[open], .sheet::backdrop { animation: none; }
  .card, .btn-round { transition: none; }
}
.grabber { width: 36px; height: 5px; border-radius: 3px; background: var(--label-2); opacity: .4; margin: 6px auto 0; flex-shrink: 0; touch-action: none; }
#sheet-body { display: flex; flex-direction: column; min-height: 0; flex: 1; }
@media (min-width: 900px) {
  .sheet { max-width: 560px; margin: auto; border-radius: 16px; max-height: 86dvh; }
  .grabber { display: none; }
  @keyframes sheet-up { from { transform: scale(.96); opacity: 0; } }
}

.sheet-bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 10px 8px 8px;
  flex-shrink: 0;
}
.sheet-bar h2 { margin: 0; font-size: 17px; font-weight: 600; text-align: center; }
.sheet-bar .start { justify-self: start; }
.sheet-bar .end { justify-self: end; }
.bar-btn {
  border: 0; background: none;
  color: var(--accent);
  min-height: 44px; padding: 0 10px;
  font-size: 17px;
  display: inline-flex; align-items: center; gap: 4px;
}
.bar-btn.strong { font-weight: 600; }
.bar-btn:disabled { opacity: .4; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 50%;
  border: 0; background: var(--fill); color: var(--label-2);
  display: grid; place-items: center;
  margin: 4px;
  position: relative;
}
.icon-btn::after { content: ""; position: absolute; inset: -4px; }
.icon-btn svg { width: 18px; height: 18px; }

.sheet-scroll {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 16px calc(env(safe-area-inset-bottom) + 24px);
  overscroll-behavior: contain;
}

/* Form */
.form-group {
  background: var(--surface);
  border-radius: 12px;
  margin-bottom: 22px;
  overflow: hidden;
}
@media (prefers-color-scheme: dark) { .form-group { background: #2C2C2E; } }
.field {
  display: flex; align-items: center; gap: 12px;
  min-height: 48px;
  padding: 0 16px;
  position: relative;
}
.field + .field::before {
  content: ""; position: absolute; top: 0; inset-inline: 16px 0;
  border-top: 1px solid var(--separator);
}
.field label { flex-shrink: 0; width: 84px; font-size: 17px; }
.field input, .field textarea, .field select {
  flex: 1; min-width: 0;
  border: 0; background: none; outline: none;
  font-size: 17px;
  padding: 12px 0;
}
.field input[type="date"] { text-align: start; min-height: 24px; }
.field.stack { flex-direction: column; align-items: stretch; gap: 0; padding-top: 10px; }
.field.stack label { width: auto; font-size: 13px; font-weight: 600; color: var(--label-2); }
.field textarea { resize: none; min-height: 96px; line-height: 1.45; padding: 6px 0 12px; field-sizing: content; }
.hint { font-size: 13px; color: var(--label-2); margin: -16px 16px 22px; }
.section-label { font-size: 13px; font-weight: 600; color: var(--label-2); margin: 0 16px 6px; }

/* Files */
.files { display: grid; gap: 0; }
.file-row {
  display: flex; align-items: center; gap: 12px;
  min-height: 52px; padding: 6px 16px;
  position: relative;
}
.file-row + .file-row::before { content: ""; position: absolute; top: 0; inset-inline: 60px 0; border-top: 1px solid var(--separator); }
.file-icon {
  width: 34px; height: 34px; border-radius: 8px;
  background: var(--accent-soft); color: var(--accent);
  display: grid; place-items: center; flex-shrink: 0;
  overflow: hidden;
}
.file-icon svg { width: 18px; height: 18px; }
.file-icon img { width: 100%; height: 100%; object-fit: cover; }
.file-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-name small { display: block; font-size: 13px; color: var(--label-2); }
.file-row a, .file-row .link { color: var(--accent); font-weight: 600; text-decoration: none; border: 0; background: none; min-height: 44px; padding: 0 6px; display: inline-flex; align-items: center; font-size: 15px; }
.add-file {
  display: flex; align-items: center; gap: 10px;
  min-height: 48px; padding: 0 16px;
  color: var(--accent);
  cursor: pointer;
  position: relative;
}
.files:not(:empty) + .add-file::before { content: ""; position: absolute; top: 0; inset-inline: 16px 0; border-top: 1px solid var(--separator); }
.add-file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.add-file svg { width: 20px; height: 20px; }

/* Detail */
.detail-head { padding: 6px 4px 18px; display: flex; gap: 14px; align-items: center; }
.detail-head .days { width: 72px; height: 72px; }
.detail-head h3 { margin: 0; font-size: 24px; line-height: 1.2; font-weight: 700; }
.detail-head p { margin: 2px 0 0; color: var(--label-2); font-size: 15px; }

.topic {
  display: flex; align-items: center; gap: 12px;
  min-height: 48px; padding: 8px 16px;
  position: relative;
  width: 100%;
  border: 0; background: none; text-align: start;
  font-size: 17px;
}
.topic + .topic::before { content: ""; position: absolute; top: 0; inset-inline: 56px 0; border-top: 1px solid var(--separator); }
.topic .check { width: 26px; height: 26px; }
.topic .check::after { display: none; }
.topic.done span { color: var(--label-2); text-decoration: line-through; }
.topic-progress { display: flex; align-items: center; gap: 10px; margin: 0 16px 8px; font-size: 13px; color: var(--label-2); font-weight: 600; }
.topic-progress .progress { flex: 1; margin: 0; height: 6px; }

.notes { white-space: pre-wrap; padding: 14px 16px; margin: 0; line-height: 1.5; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 13px 16px; position: relative; }
.kv + .kv::before { content: ""; position: absolute; top: 0; inset-inline: 16px 0; border-top: 1px solid var(--separator); }
.kv span:first-child { color: var(--label-2); }
.kv span:last-child { text-align: end; white-space: pre-wrap; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 4px; }
.actions .btn { width: 100%; }

.preview-img { width: 100%; border-radius: 12px; display: block; margin-top: 8px; }

/* Toast */
.toast {
  position: fixed;
  inset-inline: 0;
  top: calc(env(safe-area-inset-top) + 12px);
  margin: 0 auto;
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--glass);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid var(--glass-border);
  box-shadow: var(--shadow);
  font-size: 15px; font-weight: 600;
  opacity: 0;
  transform: translateY(-12px);
  transition: opacity .2s, transform .2s;
  pointer-events: none;
  z-index: 100;
}
.toast.show { opacity: 1; transform: none; }
.toast.error { color: var(--red); }

.skeleton { height: 82px; border-radius: var(--radius); background: var(--surface); opacity: .6; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .3; } }
@media (prefers-reduced-motion: reduce) { .skeleton { animation: none; } }

/* ---------- Test type picker ---------- */
.type-picker { margin-bottom: 22px; }
.type-picker label { display: grid; position: relative; cursor: pointer; }
.type-picker input { position: absolute; opacity: 0; pointer-events: none; }
.type-picker span {
  display: grid; place-items: center;
  height: 34px; padding: 0 4px;
  border-radius: 8px;
  font-size: 14px; font-weight: 500;
  white-space: nowrap;
}
.type-picker input:checked + span { background: var(--surface); box-shadow: 0 2px 6px rgba(0,0,0,.12); font-weight: 600; }
.type-picker input:focus-visible + span { outline: 3px solid var(--accent); }
@media (prefers-color-scheme: dark) { .type-picker input:checked + span { background: #636366; } }

/* ---------- Line editor (checkbox or plain text per line) ---------- */
.line-row {
  display: flex; align-items: center; gap: 4px;
  min-height: 48px;
  padding-inline: 4px 10px;
  position: relative;
}
.line-row + .line-row::before,
.line-row + .add-line::before {
  content: ""; position: absolute; top: 0; inset-inline: 52px 0;
  border-top: 1px solid var(--separator);
}
.kind {
  width: 44px; height: 44px;
  border: 0; background: none;
  display: grid; place-items: center;
  color: var(--label-2);
  flex-shrink: 0;
  border-radius: 10px;
}
.kind svg { width: 24px; height: 24px; }
.kind[aria-pressed="true"] { color: var(--accent); }
@media (hover: hover) { .kind:hover { background: var(--fill); } }
.line-input {
  flex: 1; min-width: 0;
  border: 0; background: none; outline: none;
  font-size: 17px; padding: 12px 0;
}
.line-input::placeholder { color: var(--label-2); opacity: .7; }
.icon-btn.small { width: 26px; height: 26px; margin: 0; flex-shrink: 0; }
.icon-btn.small svg { width: 14px; height: 14px; }
.icon-btn.small::after { inset: -9px; }
.line-row:not(:focus-within) .icon-btn.small { opacity: .55; }
.add-line {
  display: flex; align-items: center; gap: 10px;
  width: 100%; min-height: 48px;
  padding-inline: 16px;
  border: 0; background: none;
  color: var(--accent); font-size: 17px;
  text-align: start;
  position: relative;
}
.add-line svg { width: 20px; height: 20px; }

.topic.plain { cursor: default; }
.topic .dot { width: 26px; display: grid; place-items: center; flex-shrink: 0; }
.topic .dot::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--label-2); }

/* ---------- Subject + date filters ---------- */
.filters { display: grid; gap: 10px; min-width: 0; }
.filter-row {
  display: flex; align-items: center; gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  margin-inline: -16px;
  padding: 2px 16px;
}
.filter-row::-webkit-scrollbar { display: none; }
.chips { display: flex; gap: 8px; }
.chip {
  flex-shrink: 0;
  height: 34px;
  padding-inline: 14px;
  border-radius: 999px;
  border: 1px solid var(--separator);
  background: var(--surface);
  font-size: 15px; font-weight: 500;
  white-space: nowrap;
  display: inline-flex; align-items: center; gap: 6px;
  position: relative;
}
.chip::after { content: ""; position: absolute; inset: -5px -2px; }
.chip[aria-pressed="true"], .chip.active {
  background: var(--accent); border-color: var(--accent); color: #fff;
}
.chip-divider { width: 1px; height: 22px; background: var(--separator); flex-shrink: 0; }
.select-chip { padding-inline: 12px 10px; cursor: pointer; }
.select-chip svg { width: 17px; height: 17px; }
.select-chip .chev { width: 14px; height: 14px; opacity: .7; }
/* the real <select> covers the whole chip (invisible) so any tap opens it */
.select-chip select {
  position: absolute; inset: 0; z-index: 1;
  width: 100%; height: 100%;
  opacity: 0;
  font-size: 16px;
  cursor: pointer;
}
.select-chip:focus-within { outline: 3px solid var(--accent); outline-offset: 2px; }
.select-chip option { color: var(--label); background: var(--surface); }
.custom-range { display: flex; gap: 10px; flex-wrap: wrap; }
.custom-range[hidden] { display: none; }
.custom-range label {
  flex: 1; min-width: 140px;
  display: flex; align-items: center; gap: 8px;
  height: 40px; padding-inline: 12px;
  background: var(--fill); border-radius: 10px;
  font-size: 15px; color: var(--label-2);
}
.custom-range input {
  flex: 1; min-width: 0; height: 100%;
  border: 0; background: none; outline: none;
  font-size: 16px; color: var(--label);
  -webkit-appearance: none; appearance: none;
  display: flex; align-items: center;
}
/* iOS Safari collapses empty date inputs and centers their text */
input[type="date"] { min-height: 1.6em; }
input[type="date"]::-webkit-date-and-time-value { text-align: start; }
.empty .btn + .btn { margin-inline-start: 8px; }

@media (min-width: 900px) {
  .filters { grid-column: 1 / -1; }
  .filter-row { margin-inline: 0; padding-inline: 0; flex-wrap: wrap; overflow: visible; }
  .chips { flex-wrap: wrap; }
}
