:root {
  color-scheme: light;
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-soft: #eef2f4;
  --text: #101828;
  --muted: #667085;
  --line: #e4e9ec;
  --primary: #0f766e;
  --primary-hover: #0d635c;
  --primary-soft: #e0f0ee;
  --primary-ink: #ffffff;
  --danger: #dc2640;
  --danger-soft: #fdecef;
  --warn: #b45309;
  --warn-soft: #fdf1e0;
  --easy: #0369a1;
  --easy-soft: #e3f2fb;
  --accent: #2563eb;
  --topbar-bg: rgba(255, 255, 255, 0.85);
  --tabbar-bg: rgba(255, 255, 255, 0.94);
  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 9px;
  --shadow: 0 8px 28px rgba(16, 24, 40, 0.07);
  --soft-shadow: 0 2px 10px rgba(16, 24, 40, 0.05);
  --nav-height: 64px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1315;
  --surface: #171e21;
  --surface-soft: #212a2e;
  --text: #e8edef;
  --muted: #94a3a9;
  --line: #2a3439;
  --primary: #22b8a8;
  --primary-hover: #2ccfbd;
  --primary-soft: rgba(34, 184, 168, 0.16);
  --primary-ink: #05201c;
  --danger: #f25d72;
  --danger-soft: rgba(242, 93, 114, 0.14);
  --warn: #e8a33d;
  --warn-soft: rgba(232, 163, 61, 0.14);
  --easy: #64b5e8;
  --easy-soft: rgba(100, 181, 232, 0.14);
  --topbar-bg: rgba(20, 26, 29, 0.85);
  --tabbar-bg: rgba(20, 26, 29, 0.95);
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  --soft-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button,
input,
select,
textarea {
  font: inherit;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

.hint {
  color: var(--muted);
  font-size: 0.9rem;
}

.hidden {
  display: none !important;
}

/* ---------- Buttons ---------- */

button {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  padding: 0 16px;
  cursor: pointer;
  font-weight: 600;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.1s ease;
}

button:hover {
  border-color: var(--primary);
}

button:active {
  transform: scale(0.98);
}

.primary {
  background: var(--primary);
  color: var(--primary-ink);
  border-color: var(--primary);
}

.primary:hover {
  background: var(--primary-hover);
}

.danger {
  color: #fff;
  background: var(--danger);
  border-color: var(--danger);
}

.ghost {
  background: transparent;
}

/* ---------- Top bar & icon navigation ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px clamp(12px, 4vw, 32px);
  border-bottom: 1px solid var(--line);
  background: var(--topbar-bg);
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: blur(12px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  flex: none;
  border-radius: var(--radius-md);
  background: var(--primary);
  color: #fff;
}

.brand-mark svg {
  width: 22px;
  height: 22px;
}

h1 {
  margin-bottom: 0;
  font-size: 1.05rem;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

.brand-copy p {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tabs {
  display: flex;
  gap: 4px;
}

.tab {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 3px;
  min-width: 64px;
  min-height: 52px;
  padding: 6px 10px;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--muted);
  font-weight: 600;
}

.tab svg {
  width: 23px;
  height: 23px;
}

.tab-label {
  font-size: 0.66rem;
  letter-spacing: 0.02em;
}

.tab:hover {
  background: var(--surface-soft);
  color: var(--text);
}

.tab.active {
  background: var(--primary-soft);
  color: var(--primary);
}

/* ---------- Layout ---------- */

main {
  width: min(1200px, 100%);
  margin: 0 auto;
  padding: 16px clamp(12px, 4vw, 32px) 40px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.panel,
.flashcard,
.empty-state {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--soft-shadow);
}

.panel {
  padding: 18px;
}

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

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

/* ---------- Forms ---------- */

.field {
  display: grid;
  gap: 6px;
  margin: 10px 0;
}

.field > span {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  padding: 11px 12px;
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

/* ---------- Profile gate ---------- */

.profile-welcome {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) minmax(300px, 420px);
  gap: 16px;
  align-items: stretch;
  max-width: 920px;
  margin: 4vh auto 16px;
}

.profile-copy {
  display: grid;
  align-content: center;
  padding: 26px;
}

.profile-copy h2 {
  margin-bottom: 10px;
  font-size: clamp(1.7rem, 3.6vw, 2.6rem);
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.eyebrow {
  margin-bottom: 8px;
  color: var(--primary);
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.profile-form {
  align-content: start;
}

.profile-mode {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  margin-bottom: 10px;
  border-radius: var(--radius-md);
  background: var(--surface-soft);
}

.mode-btn {
  min-height: 38px;
  border: none;
  background: transparent;
  color: var(--muted);
}

.mode-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--soft-shadow);
}

.wide-button {
  width: 100%;
  margin-top: 8px;
}

/* ---------- Active profile bar ---------- */

.active-profile {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  max-width: 860px;
  margin: 0 auto 12px;
}

.active-profile-id {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.avatar {
  width: 34px;
  height: 34px;
  flex: none;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, #14b8a6, var(--primary));
}

.active-profile strong {
  display: block;
  font-size: 0.95rem;
}

.active-profile .hint {
  margin: 0;
  font-size: 0.78rem;
}

.active-profile .ghost {
  min-height: 36px;
  padding: 0 12px;
  font-size: 0.85rem;
}

/* ---------- Study layout ---------- */

.study-layout {
  display: grid;
  gap: 12px;
  max-width: 860px;
  margin: 0 auto;
}

.session-strip {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr);
  gap: 12px;
  align-items: center;
}

.goal-card {
  display: grid;
  gap: 8px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--soft-shadow);
}

.goal-copy {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.goal-copy span {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
}

.goal-card strong {
  font-size: 1.25rem;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.stat-chip {
  display: grid;
  justify-items: center;
  gap: 2px;
  padding: 10px 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--soft-shadow);
}

.stat-chip strong {
  font-size: 1.2rem;
}

.stat-chip span {
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* ---------- Flashcard ---------- */

.flashcard,
.empty-state {
  display: grid;
  align-content: center;
  justify-items: center;
  min-height: clamp(320px, 46vh, 460px);
  padding: clamp(20px, 4vw, 40px);
  text-align: center;
}

.card-meta {
  display: grid;
  justify-items: center;
  gap: 8px;
  margin-bottom: 14px;
}

.box-label {
  margin: 0;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.box-steps {
  display: flex;
  gap: 5px;
}

.box-step {
  width: 22px;
  height: 6px;
  border-radius: 999px;
  background: var(--surface-soft);
}

.box-step.done {
  background: var(--primary);
}

.box-step.current {
  background: var(--primary);
  opacity: 0.45;
}

.flashcard h2 {
  margin-bottom: 22px;
  font-size: clamp(2.1rem, 7vw, 4rem);
  letter-spacing: -0.02em;
  overflow-wrap: anywhere;
}

.reveal-button {
  min-width: min(280px, 100%);
  min-height: 50px;
}

.answer {
  width: min(620px, 100%);
}

.answer dl {
  display: grid;
  grid-template-columns: 130px 1fr;
  gap: 8px 16px;
  margin: 0 0 18px;
  padding: 14px 16px;
  border-radius: var(--radius-md);
  background: var(--surface-soft);
  text-align: left;
}

.answer dt {
  color: var(--muted);
  font-size: 0.88rem;
}

.answer dd {
  margin: 0;
  font-weight: 600;
}

.review-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  width: 100%;
}

.grade {
  display: grid;
  justify-items: center;
  gap: 2px;
  min-height: 56px;
  padding: 8px 4px;
  font-size: 0.85rem;
}

.grade em {
  font-style: normal;
  font-weight: 800;
  font-size: 1rem;
}

.grade-again {
  background: var(--danger-soft);
  border-color: transparent;
  color: var(--danger);
}

.grade-hard {
  background: var(--warn-soft);
  border-color: transparent;
  color: var(--warn);
}

.grade-good {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-ink);
}

.grade-easy {
  background: var(--easy-soft);
  border-color: transparent;
  color: var(--easy);
}

.grade-hint {
  margin: 12px 0 0;
  font-size: 0.78rem;
}

.empty-state h2 {
  font-size: 1.4rem;
}

.empty-state p {
  max-width: 420px;
  color: var(--muted);
}

/* ---------- Up next ---------- */

.up-next h3 {
  margin-bottom: 10px;
  font-size: 0.95rem;
}

.up-next-list {
  display: grid;
  gap: 6px;
}

.up-next-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 46px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
  text-align: left;
  font-weight: 600;
}

.up-next-item .word {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.up-next-item .pill {
  flex: none;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
}

.up-next-empty {
  padding: 10px 4px;
  color: var(--muted);
  font-size: 0.88rem;
}

/* ---------- Study options ---------- */

.study-options summary {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 30px;
  cursor: pointer;
  font-weight: 700;
  list-style: none;
}

.study-options summary::-webkit-details-marker {
  display: none;
}

.study-options summary svg {
  width: 18px;
  height: 18px;
  color: var(--muted);
}

.study-options summary .chevron {
  margin-left: auto;
  transition: transform 0.2s ease;
}

.study-options[open] summary .chevron {
  transform: rotate(180deg);
}

.study-options[open] summary {
  margin-bottom: 12px;
}

.mode-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  min-height: 40px;
  padding: 0 16px;
  border-radius: 999px;
  font-size: 0.9rem;
}

.chip.active {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-ink);
}

.study-controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 4px 10px;
  margin-top: 6px;
}

.study-controls .field {
  margin: 8px 0 0;
}

/* ---------- Theme toggle ---------- */

.theme-toggle {
  flex: none;
  width: 44px;
  min-height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--muted);
}

.theme-toggle:hover {
  background: var(--surface-soft);
  color: var(--text);
}

.theme-toggle svg {
  width: 21px;
  height: 21px;
}

.theme-toggle .sun {
  display: none;
}

:root[data-theme="dark"] .theme-toggle .sun {
  display: block;
}

:root[data-theme="dark"] .theme-toggle .moon {
  display: none;
}

/* ---------- Manual review ---------- */

.manual-label-filter {
  width: min(280px, 100%);
  margin: 0;
}

.review-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 14px;
}

.manual-filter {
  min-height: 38px;
  padding: 0 14px;
  border-radius: 999px;
  font-size: 0.88rem;
}

.manual-filter.active {
  background: var(--text);
  border-color: var(--text);
  color: var(--surface);
}

.manual-list {
  display: grid;
  gap: 8px;
}

.manual-empty {
  padding: 16px;
  border: 1px dashed var(--line);
  border-radius: var(--radius-md);
  background: var(--surface-soft);
}

.manual-empty strong {
  font-size: 1rem;
}

.manual-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
}

.manual-word {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.manual-word strong {
  font-size: 1.05rem;
  overflow-wrap: anywhere;
}

.manual-word span {
  color: var(--muted);
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.manual-load-more {
  width: 100%;
}

/* ---------- Tracker ---------- */

.tracker-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(120px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.stat-tile {
  display: grid;
  gap: 4px;
  padding: 14px;
}

.stat-tile strong {
  font-size: 1.5rem;
}

.stat-tile span {
  color: var(--muted);
  font-size: 0.8rem;
}

.progress-panels {
  display: grid;
  grid-template-columns: repeat(2, minmax(220px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.progress-panels + .panel,
.panel + .panel {
  margin-top: 12px;
}

.progress-panels .panel {
  margin-top: 0;
}

.quest-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.quest-row span {
  color: var(--muted);
}

.quest-row strong {
  font-size: 0.95rem;
}

.bar {
  height: 10px;
  background: var(--surface-soft);
  border-radius: 999px;
  overflow: hidden;
}

.bar i {
  display: block;
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--primary), #14b8a6);
  transition: width 0.3s ease;
}

.week-dots,
.badge-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.week-dot,
.badge {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  padding: 7px 12px;
  color: var(--muted);
  font-size: 0.85rem;
}

.week-dot.done,
.badge.earned {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 600;
}

.box-bars {
  display: grid;
  gap: 12px;
}

.box-row {
  display: grid;
  grid-template-columns: 64px 1fr 44px;
  gap: 12px;
  align-items: center;
}

.box-row span {
  color: var(--muted);
  font-size: 0.88rem;
}

/* ---------- Admin & profile ---------- */

.admin-login {
  max-width: 480px;
  margin: 4vh auto 0;
}

.profile-panel {
  max-width: 680px;
}

.table-wrap {
  overflow: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px;
  text-align: left;
  vertical-align: top;
  font-size: 0.92rem;
}

.debug-panel {
  margin-top: 18px;
}

#debugLog {
  min-height: 200px;
  max-height: 400px;
  overflow: auto;
  margin: 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: #111814;
  color: #d7f7df;
  font-size: 0.85rem;
  white-space: pre-wrap;
}

/* ---------- Tablet ---------- */

@media (max-width: 1000px) {
  .tracker-grid {
    grid-template-columns: repeat(3, minmax(130px, 1fr));
  }
}

/* ---------- Mobile: bottom icon navigation + focused study flow ---------- */

@media (max-width: 700px) {
  main {
    padding: 12px 12px calc(var(--nav-height) + 24px);
  }

  .topbar {
    padding: 8px 12px;
    /* backdrop-filter would become the containing block for the
       fixed bottom nav, anchoring it to the header instead of the viewport */
    backdrop-filter: none;
    background: var(--surface);
  }

  .tabs {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    justify-content: space-around;
    gap: 0;
    height: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px));
    padding: 4px 6px calc(4px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--line);
    background: var(--tabbar-bg);
    backdrop-filter: blur(14px);
  }

  .tab {
    flex: 1;
    min-width: 0;
    min-height: 0;
    border-radius: var(--radius-sm);
  }

  .profile-welcome {
    grid-template-columns: 1fr;
    gap: 12px;
    margin-top: 8px;
  }

  .profile-copy {
    padding: 18px;
  }

  .active-profile {
    padding: 10px 12px;
  }

  .active-profile .hint {
    display: none;
  }

  .session-strip {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .goal-card {
    padding: 12px 14px;
  }

  .flashcard,
  .empty-state {
    min-height: clamp(280px, 42vh, 420px);
    padding: 20px 14px;
  }

  .flashcard h2 {
    font-size: clamp(1.9rem, 10vw, 3rem);
  }

  .review-actions {
    grid-template-columns: repeat(2, 1fr);
  }

  .answer dl {
    grid-template-columns: 1fr;
    gap: 2px 0;
  }

  .answer dt {
    margin-top: 8px;
  }

  .panel {
    padding: 14px;
  }

  .tracker-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .progress-panels {
    grid-template-columns: 1fr;
  }

  .manual-row {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .manual-row button {
    width: 100%;
  }
}

@media (max-width: 380px) {
  .stat-chip strong {
    font-size: 1.05rem;
  }

  .grade {
    font-size: 0.78rem;
  }
}
