@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

/* ── Design tokens ─────────────────────────────────────────────────────────── */
:root {
  --bg:          #f1f4f9;
  --surface:     #ffffff;
  --surface-2:   #f8fafc;
  --border:      #e2e8f4;
  --border-hover:#c7d2e8;
  --text:        #0f172a;
  --text-2:      #334155;
  --muted:       #64748b;
  --accent:      #4f46e5;
  --accent-h:    #4338ca;
  --accent-soft: #eef2ff;
  --green:       #059669;
  --green-soft:  #d1fae5;
  --red:         #dc2626;
  --red-soft:    #fee2e2;
  --orange:      #d97706;
  --orange-soft: #fef3c7;
  --shadow-xs:   0 1px 2px rgba(15,23,42,.06);
  --shadow-sm:   0 2px 8px rgba(15,23,42,.08);
  --shadow-md:   0 4px 16px rgba(15,23,42,.10);
  --r-sm:        8px;
  --r-md:        12px;
  --r-lg:        16px;
  --r-full:      9999px;
}

/* ── Reset ──────────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ─────────────────────────────────────────────────────────────────── */
.container { max-width: 880px; margin: 0 auto; padding: 1.75rem 1.25rem; }
.container.narrow { max-width: 460px; }

h1 { font-size: 1.4rem; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: .95rem; font-weight: 600; color: var(--text-2); }

/* ── Card ───────────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border-radius: var(--r-md);
  padding: 1.5rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
}

/* ── Topbar ─────────────────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.topbar h1 { margin: 0; }

.back-link {
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  color: var(--muted);
  text-decoration: none;
  font-size: .88rem;
  font-weight: 500;
  transition: color .15s;
}
.back-link:hover { color: var(--accent); }

/* ── Buttons ────────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  padding: .55rem 1.25rem;
  background: var(--accent);
  color: #fff;
  border: 1.5px solid var(--accent);
  border-radius: var(--r-sm);
  font-family: inherit;
  font-size: .88rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background .15s, border-color .15s, box-shadow .15s, transform .1s;
  white-space: nowrap;
  letter-spacing: .01em;
}
.btn:hover  { background: var(--accent-h); border-color: var(--accent-h); box-shadow: 0 2px 8px rgba(79,70,229,.25); }
.btn:active { transform: translateY(1px); }

.btn-lg { padding: .7rem 1.6rem; font-size: .95rem; border-radius: var(--r-sm); }
.btn-sm { padding: .35rem .85rem; font-size: .8rem; border-radius: 6px; }

.btn-outline {
  background: transparent;
  color: var(--accent);
  border-color: var(--border);
}
.btn-outline:hover { background: var(--accent-soft); border-color: var(--accent); box-shadow: none; }

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border-color: transparent;
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); box-shadow: none; }

.btn-danger { background: var(--red); border-color: var(--red); }
.btn-danger:hover { background: #b91c1c; border-color: #b91c1c; box-shadow: 0 2px 8px rgba(220,38,38,.25); }

.btn-group { display: flex; gap: .6rem; flex-wrap: wrap; }

/* ── Notices ────────────────────────────────────────────────────────────────── */
.notice {
  display: flex; align-items: flex-start; gap: .6rem;
  background: var(--accent-soft);
  border: 1px solid #c7d2fe;
  border-radius: var(--r-sm);
  padding: .75rem 1rem;
  margin-bottom: 1rem;
  font-size: .88rem;
  color: #3730a3;
}
.success { color: var(--green); font-size: .95rem; font-weight: 500; margin-bottom: .75rem; }
.error   { color: var(--red);   font-size: .95rem; font-weight: 500; margin-bottom: .75rem; }
.muted   { color: var(--muted); font-size: .88rem; }

.error-banner {
  display: flex; align-items: center; gap: .5rem;
  background: var(--red-soft);
  border: 1px solid #fca5a5;
  color: #991b1b;
  border-radius: var(--r-sm);
  padding: .65rem 1rem;
  margin-bottom: 1rem;
  font-size: .88rem;
  font-weight: 500;
}

/* ── Inline form ────────────────────────────────────────────────────────────── */
.inline-form { display: flex; gap: .5rem; flex-wrap: wrap; }
.inline-form input[type=text] {
  flex: 1; min-width: 180px;
  padding: .5rem .75rem;
  border: 1.5px solid var(--border);
  border-radius: var(--r-sm);
  font-size: .9rem;
  font-family: inherit;
  background: var(--surface-2);
  transition: border-color .15s, box-shadow .15s;
}
.inline-form input[type=text]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79,70,229,.1);
  background: #fff;
}

/* ── Auth ───────────────────────────────────────────────────────────────────── */
.auth-card {
  margin-top: 5rem;
  padding: 2.5rem;
}
.auth-card h1 { margin-bottom: 1.5rem; font-size: 1.5rem; }

.auth-card label,
.card label {
  display: flex; flex-direction: column; gap: .4rem;
  font-size: .85rem; font-weight: 600; color: var(--text-2);
  margin-bottom: .9rem;
}

.auth-card input[type=text],
.auth-card input[type=password],
.card input[type=text],
.card input[type=password] {
  padding: .6rem .85rem;
  border: 1.5px solid var(--border);
  border-radius: var(--r-sm);
  font-size: .95rem;
  font-family: inherit;
  background: var(--surface-2);
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
.auth-card input:focus,
.card input[type=text]:focus,
.card input[type=password]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79,70,229,.1);
  background: #fff;
}
.auth-card .btn-lg { width: 100%; margin-top: .25rem; }

/* ── Stats row ──────────────────────────────────────────────────────────────── */
.stats-row { display: flex; gap: .75rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.stat-box {
  flex: 1; min-width: 90px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 1rem 1.25rem;
  text-align: center;
  box-shadow: var(--shadow-xs);
  display: flex; flex-direction: column; gap: .15rem;
}
.stat-box.correct  { border-top: 3px solid var(--green); }
.stat-box.incorrect{ border-top: 3px solid var(--red); }
.stat-num   { font-size: 1.75rem; font-weight: 700; letter-spacing: -.02em; }
.stat-label { font-size: .78rem; color: var(--muted); font-weight: 500; text-transform: uppercase; letter-spacing: .03em; }

/* ── Test header ────────────────────────────────────────────────────────────── */
.test-header {
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: .5rem;
  margin-bottom: 1.25rem;
}
.test-stats { display: flex; gap: .4rem; flex-wrap: wrap; }

/* ── Badges ─────────────────────────────────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center;
  padding: .25rem .7rem;
  border-radius: var(--r-full);
  font-size: .78rem;
  font-weight: 600;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-2);
  letter-spacing: .01em;
}
.badge-green { background: var(--green-soft); border-color: #6ee7b7; color: #065f46; }
.badge-red   { background: var(--red-soft);   border-color: #fca5a5; color: #991b1b; }

/* ── Question card ──────────────────────────────────────────────────────────── */
.question-card { border-left: 3px solid var(--accent); }
.question-meta {
  display: flex; align-items: center; gap: .4rem; flex-wrap: wrap;
  font-size: .8rem; color: var(--muted); margin-bottom: .75rem;
  font-weight: 500;
}
.fav-btn {
  margin-left: auto;
  font-size: 1.25rem;
  line-height: 1;
  padding: .25rem .55rem;
  min-height: 32px;
  flex-shrink: 0;
}
.fav-btn[data-state="1"] { color: var(--orange); }
.type-badge {
  display: inline-flex; align-items: center;
  padding: .15rem .55rem;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: var(--r-full);
  font-size: .73rem;
  font-weight: 600;
  letter-spacing: .02em;
}
.question-text { font-size: 1.05rem; line-height: 1.65; color: var(--text); }

/* ── Protocol result badge ──────────────────────────────────────────────────── */
.protocol-result-badge {
  display: inline-flex; align-items: center; gap: .3rem;
  margin-top: .75rem;
  padding: .25rem .7rem;
  border-radius: var(--r-full);
  font-size: .78rem;
  font-weight: 600;
  border: 1px solid transparent;
}
.pr-correct   { background: var(--green-soft); border-color: #6ee7b7; color: #065f46; }
.pr-incorrect { background: var(--red-soft);   border-color: #fca5a5; color: #991b1b; }

/* ── Options ────────────────────────────────────────────────────────────────── */
.options-list { display: flex; flex-direction: column; gap: .45rem; margin-bottom: 1.25rem; }

.option-label {
  display: flex; align-items: flex-start; gap: .85rem;
  padding: .85rem 1.1rem;
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: border-color .15s, background .15s, box-shadow .15s;
}
.option-label:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: 0 0 0 3px rgba(79,70,229,.07);
}
/* Hide native input, keep it accessible */
.option-label input[type=radio],
.option-label input[type=checkbox] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  pointer-events: none;
}

/* Custom indicator */
.opt-indicator {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  margin-top: .18rem;
  border: 2px solid var(--border-hover);
  background: var(--surface);
  transition: border-color .15s, background .15s, box-shadow .15s;
  position: relative;
}

/* Radio → circle */
input[type=radio]  ~ .opt-indicator { border-radius: 50%; }
/* Checkbox → rounded square */
input[type=checkbox] ~ .opt-indicator { border-radius: 5px; }

/* Hover state (via parent label hover) */
.option-label:hover .opt-indicator {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79,70,229,.12);
}

/* Checked — fill */
input:checked ~ .opt-indicator {
  background: var(--accent);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79,70,229,.15);
}

/* Radio dot */
input[type=radio]:checked ~ .opt-indicator::after {
  content: '';
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 7px; height: 7px;
  border-radius: 50%;
  background: #fff;
}

/* Checkbox checkmark */
input[type=checkbox]:checked ~ .opt-indicator::after {
  content: '';
  position: absolute;
  top: 2px; left: 6px;
  width: 5px; height: 9px;
  border: 2.5px solid #fff;
  border-top: none;
  border-left: none;
  transform: rotate(45deg);
}

.option-text { font-size: .95rem; line-height: 1.55; }

/* ── Result option states ───────────────────────────────────────────────────── */
.option-result {
  display: flex; align-items: flex-start; gap: .85rem;
  padding: .85rem 1.1rem;
  border: 1.5px solid var(--border);
  border-radius: var(--r-sm);
}
.opt-correct-selected { background: var(--green-soft); border-color: #6ee7b7; }
.opt-correct-missed   { background: var(--orange-soft); border-color: #fcd34d; }
.opt-wrong-selected   { background: var(--red-soft);   border-color: #fca5a5; }
.option-icon { font-weight: 700; min-width: 1rem; flex-shrink: 0; margin-top: .15rem; }

/* ── Result banner ──────────────────────────────────────────────────────────── */
.result-banner {
  display: flex; align-items: center; gap: .5rem;
  padding: .85rem 1.2rem;
  border-radius: var(--r-sm);
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 1rem;
  border: 1.5px solid transparent;
}
.result-correct   { background: var(--green-soft); border-color: #6ee7b7; color: #065f46; }
.result-incorrect { background: var(--red-soft);   border-color: #fca5a5; color: #991b1b; }
.result-neutral   { background: var(--surface-2);  border-color: var(--border); color: var(--muted); }

/* ── Form actions ───────────────────────────────────────────────────────────── */
.form-actions { display: flex; gap: .6rem; flex-wrap: wrap; margin-top: .75rem; align-items: center; }
.form-actions .btn-secondary-action { flex: 0 0 auto; }

/* ── Test card actions (two-button row inside dashboard cards) ─────────────── */
.test-card-actions {
  display: flex;
  gap: .5rem;
  flex-wrap: wrap;
  width: 100%;
}
.test-card-actions > .btn { flex: 1 1 auto; }

/* ── Progress bar ───────────────────────────────────────────────────────────── */
.progress-wrap { display: flex; flex-direction: column; gap: .75rem; }
.progress-bar  { height: 6px; background: var(--border); border-radius: var(--r-full); overflow: hidden; }
.progress-fill { height: 100%; background: var(--accent); border-radius: var(--r-full); transition: width .4s ease; }

/* ── User list ──────────────────────────────────────────────────────────────── */
.user-list { display: flex; flex-wrap: wrap; gap: .5rem; }
.user-btn {
  padding: .45rem 1rem;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: var(--r-sm);
  text-decoration: none;
  font-weight: 600;
  font-size: .88rem;
  transition: background .15s;
  border: 1px solid #c7d2fe;
}
.user-btn:hover { background: #e0e7ff; }

.user-list-plain { list-style: none; display: flex; flex-direction: column; gap: .35rem; }
.user-list-plain li {
  padding: .5rem .85rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  font-size: .9rem;
  color: var(--text-2);
}
.user-me { background: var(--accent-soft); border-color: #c7d2fe; color: var(--accent); font-weight: 600; }

/* ── Protocols list ─────────────────────────────────────────────────────────── */
.protocols-list { display: flex; flex-direction: column; gap: .6rem; }
.protocol-card {
  display: flex; align-items: center;
  justify-content: space-between; gap: 1rem;
  padding: 1rem 1.5rem;
  margin-bottom: 0;
}
.protocol-info { flex: 1; min-width: 0; }
.protocol-name { font-weight: 600; font-size: .95rem; color: var(--text); margin-bottom: .2rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.protocol-meta { font-size: .8rem; color: var(--muted); }

/* ── Stat chips (dashboard summary) ────────────────────────────────────────── */
.stat-chip {
  display: flex; flex-direction: column; align-items: center; gap: .1rem;
  padding: .55rem 1rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  min-width: 72px;
  text-align: center;
}
.stat-chip-num   { font-size: 1.4rem; font-weight: 700; letter-spacing: -.02em; line-height: 1.2; }
.stat-chip-label { font-size: .72rem; color: var(--muted); font-weight: 500; text-transform: uppercase; letter-spacing: .04em; white-space: nowrap; }

/* ── Test cards on dashboard ─────────────────────────────────────────────────── */
.test-card { padding: 1.25rem 1.5rem; }
.test-card-title {
  font-size: .95rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 1rem;
  letter-spacing: -.01em;
}
.test-card-body {
  display: flex;
  align-items: center;
  gap: .85rem;
  flex-wrap: wrap;
}

/* Count input */
.count-label {
  display: flex; align-items: center; gap: .45rem;
  font-size: .82rem; font-weight: 600; color: var(--muted);
  white-space: nowrap; flex-shrink: 0;
}
.count-label input[type=number] {
  width: 68px;
  padding: .38rem .6rem;
  border: 1.5px solid var(--border);
  border-radius: var(--r-sm);
  font-size: .9rem;
  font-family: inherit;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
.count-label input[type=number]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79,70,229,.1);
  background: #fff;
}

/* Filter pills */
.filter-group { display: flex; gap: .4rem; flex-wrap: wrap; flex: 1; }
.filter-option {
  display: inline-flex; align-items: center; gap: .35rem;
  padding: .35rem .85rem;
  border: 1.5px solid var(--border);
  border-radius: var(--r-full);
  font-size: .82rem; font-weight: 500; color: var(--muted);
  cursor: pointer;
  transition: border-color .15s, background .15s, color .15s;
  user-select: none;
  white-space: nowrap;
}
.filter-option:hover { border-color: var(--border-hover); color: var(--text-2); background: var(--surface-2); }
.filter-option input[type=radio] { display: none; }

/* Highlight selected pill via :has() */
.filter-option:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

/* Protocol progress bar in dashboard cards */
.proto-progress { width: 140px; }

/* ── Import spinner ─────────────────────────────────────────────────────────── */
@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
  width: 24px; height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  margin: 0 auto 1rem;
}

/* ── Divider ────────────────────────────────────────────────────────────────── */
.divider {
  height: 1px;
  background: var(--border);
  margin: 1.25rem 0;
}

/* ── Scrollbar (WebKit) ─────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-hover); }

/* ══════════════════════════════════════════════════════════════════════════════
   Mobile  ≤ 640px
   ══════════════════════════════════════════════════════════════════════════════ */
@media (max-width: 640px) {

  /* Layout */
  body { font-size: 14px; }
  .container { padding: 1rem .875rem; }

  h1 { font-size: 1.2rem; }

  /* Card */
  .card { padding: 1.1rem 1rem; border-radius: var(--r-sm); }

  /* Topbar */
  .topbar { gap: .5rem; margin-bottom: 1.1rem; }
  .topbar h1 { font-size: 1.1rem; }

  /* Auth */
  .auth-card { margin-top: 2rem; padding: 1.5rem 1.25rem; }

  /* Buttons — bigger touch targets */
  .btn    { padding: .6rem 1.1rem; min-height: 42px; }
  .btn-lg { padding: .75rem 1.4rem; min-height: 46px; font-size: .95rem; }
  .btn-sm { padding: .45rem .85rem; min-height: 36px; }

  /* Form actions — primary button stretches; secondary actions sit on a row */
  .form-actions { flex-direction: column; align-items: stretch; }
  .form-actions .btn-lg { width: 100%; }
  .form-actions .btn-ghost { align-self: flex-start; }
  /* "Don't know" / star sits side-by-side with the same row, not stretched */
  .form-actions .btn-secondary-action { width: 100%; }

  /* Inline form (file upload) */
  .inline-form { flex-direction: column; }
  .inline-form .btn { width: 100%; }

  /* Test header */
  .test-header { gap: .4rem; }

  /* Test card body: stack vertically */
  .test-card { padding: 1rem; }
  .test-card-body {
    flex-direction: column;
    align-items: stretch;
    gap: .75rem;
  }

  /* Count input row */
  .count-label { justify-content: space-between; }
  .count-label input[type=number] { width: 80px; }

  /* Filter pills: wrap onto multiple rows, all visible */
  .filter-group {
    flex-wrap: wrap;
    gap: .35rem;
    width: 100%;
  }
  .filter-option {
    padding: .4rem .85rem;
    min-height: 36px;
    font-size: .8rem;
  }

  /* Submit button in test card: full width */
  .test-card-body > .btn { width: 100%; justify-content: center; }

  /* Two-button row inside test card stacks vertically */
  .test-card-actions { flex-direction: column; }
  .test-card-actions > .btn { width: 100%; }

  /* Star button in question header — slightly smaller on mobile */
  .fav-btn { font-size: 1.15rem; min-height: 30px; padding: .2rem .5rem; }

  /* Stat chips: wrap to 3 columns */
  .stat-chip {
    min-width: 0;
    flex: 1 1 calc(33% - .5rem);
    padding: .5rem .6rem;
  }
  .stat-chip-num { font-size: 1.2rem; }
  .stat-chip-label { font-size: .68rem; }

  /* Protocol card: stats below title */
  .protocol-card { flex-direction: column; align-items: flex-start; }

  /* Protocol stats in test card: allow wrap */
  .test-card-title { font-size: .9rem; }

  /* Progress bar in protocol card */
  .progress-bar[style*="width:140px"] { width: 100% !important; }

  /* Options: bigger tap targets */
  .option-label { padding: .9rem 1rem; min-height: 48px; }
  .option-result { padding: .9rem 1rem; }
  .option-text { font-size: .92rem; }

  /* Result banner */
  .result-banner { font-size: .95rem; padding: .75rem 1rem; }

  /* Done page stats */
  .stats-row { gap: .6rem; }
  .stat-box { padding: .85rem .75rem; }
  .stat-num { font-size: 1.5rem; }

  /* Btn group: stack */
  .btn-group { flex-direction: column; }
  .btn-group .btn { width: 100%; }

  /* Users manage page */
  .card label { margin-bottom: .75rem; }
}

/* ── Very small screens ≤ 380px ─────────────────────────────────────────────── */
@media (max-width: 380px) {
  .container { padding: .875rem .75rem; }
  .card { padding: 1rem .875rem; }
  .auth-card { padding: 1.25rem 1rem; }
  h1 { font-size: 1.05rem; }
  .badge { font-size: .72rem; padding: .2rem .55rem; }
}
