:root {
  color-scheme: light;
  --bg-top: #f8f1e5;
  --bg-bottom: #efe0c6;
  --surface: rgba(255, 251, 244, 0.88);
  --surface-strong: rgba(255, 255, 255, 0.92);
  --border: rgba(112, 70, 38, 0.18);
  --border-strong: rgba(112, 70, 38, 0.32);
  --text: #27180f;
  --muted: #6d5847;
  --accent: #8b4d2c;
  --accent-strong: #6f3a1f;
  --accent-soft: rgba(139, 77, 44, 0.12);
  --success: #1f5e3d;
  --error: #8d2117;
  --nav-active: #264b46;
  --shadow: 0 24px 60px rgba(62, 34, 16, 0.1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  background:
    radial-gradient(circle at top left, rgba(193, 80, 40, 0.18), transparent 32%),
    linear-gradient(180deg, var(--bg-top) 0%, var(--bg-bottom) 100%);
  color: var(--text);
}

.page-shell {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 20px 56px;
}

.panel,
.callout,
.utility-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.panel {
  padding: 22px;
}

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

h2 {
  font-size: 1.5rem;
}

h3 {
  font-size: 1.1rem;
  margin-bottom: 10px;
}

h4 {
  font-size: 1rem;
  margin-bottom: 8px;
}

.section-copy,
.utility-copy {
  color: var(--muted);
  line-height: 1.6;
}

.top-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  padding: 6px;
  margin: 8px 0 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.nav-button {
  border: 1px solid transparent;
  border-radius: 0;
  padding: 10px 18px;
  background: transparent;
  color: var(--accent);
  cursor: pointer;
  letter-spacing: 0.01em;
  transition: background-color 120ms ease, color 120ms ease;
}

.nav-button:hover {
  background: var(--accent-soft);
}

.nav-button.active {
  background: var(--nav-active);
  border-color: var(--nav-active);
  color: #f7f2eb;
}

.nav-button.active:hover {
  background: var(--nav-active);
}

.grid {
  display: grid;
  grid-template-columns: minmax(280px, 330px) minmax(0, 1fr);
  gap: 20px;
}

.panel-wide {
  min-height: 520px;
}

.auth-only .grid {
  grid-template-columns: 1fr;
}

.auth-only #view-panel {
  display: none;
}

body:not(.auth-only) .grid {
  grid-template-columns: 1fr;
}

body:not(.auth-only) #auth-panel {
  display: none;
}

.panel-header,
.item-header,
.button-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.compact-row {
  justify-content: flex-end;
}

.subsection + .subsection {
  margin-top: 22px;
}

.stat-card {
  display: grid;
  gap: 6px;
  margin: 18px 0;
  padding: 22px;
  border-radius: 0;
  background: linear-gradient(135deg, rgba(139, 77, 44, 0.14), rgba(38, 75, 70, 0.12));
  border: 1px solid rgba(38, 75, 70, 0.15);
}

.stat-number {
  font-size: clamp(2.7rem, 7vw, 4.8rem);
  line-height: 0.9;
}

.stat-label {
  font-size: 1rem;
  color: var(--muted);
}

.compact-stat-card {
  margin: 0;
  padding: 16px;
}

.compact-stat-card .stat-number {
  font-size: 2rem;
}

.course-stack-stat {
  max-width: 280px;
  margin-bottom: 18px;
}

.stack-form {
  display: grid;
  gap: 10px;
  margin-bottom: 16px;
}

.stack-field {
  display: grid;
  gap: 6px;
}

.stack-field span {
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 700;
}

.exam-form-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(180px, 0.35fr);
  gap: 10px;
}

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

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

textarea {
  min-height: 92px;
  resize: vertical;
}

button {
  border: 0;
  border-radius: 0;
  padding: 11px 16px;
  background: var(--accent);
  color: #fff8f3;
  cursor: pointer;
}

button:hover {
  background: var(--accent-strong);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

button:disabled:hover {
  background: var(--accent);
}

a {
  color: var(--accent);
}

.ghost-button {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border-strong);
}

.ghost-button:disabled:hover {
  background: transparent;
}

.link-button {
  background: transparent;
  border: none;
  padding: 0;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  text-decoration: underline;
  min-height: 0;
}

.link-button:hover {
  background: transparent;
}

.study-exam-summary {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  min-width: 220px;
  max-width: 320px;
  position: relative;
}

.study-exam-switcher {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.95rem;
}

.study-exam-switcher select {
  min-width: 220px;
}

.study-exam-trigger {
  text-decoration: none;
  text-align: left;
  font-weight: 600;
  color: var(--muted);
  flex: 1 1 auto;
}

.study-exam-trigger:hover {
  color: var(--accent);
  text-decoration: underline;
}

.study-mastery-stack {
  display: grid;
  gap: 5px;
  width: 100%;
}

.study-mastery-stack--placeholder {
  height: 36px;
}

.study-mastery-bar {
  --progress: 0%;
  position: relative;
  height: 16px;
  border: 1px solid var(--border);
  overflow: hidden;
  background: rgba(139, 77, 44, 0.08);
}

.study-mastery-bar::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: var(--progress);
  background: rgba(38, 75, 70, 0.32);
}

.study-mastery-bar-label {
  position: relative;
  z-index: 1;
  display: block;
  padding: 0 8px;
  line-height: 16px;
  font-size: 0.78rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.study-exam-tags-popunder {
  position: absolute;
  top: 100%;
  right: 0;
  left: 0;
  margin-top: 6px;
  padding: 10px 12px;
  background: var(--surface-strong);
  border: 1px solid var(--border-strong);
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  z-index: 5;
  box-shadow: var(--shadow);
}

.tag-chip {
  padding: 4px 9px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.82rem;
}

.ghost-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 11px 16px;
  border-radius: 0;
  border: 1px solid var(--border-strong);
  text-decoration: none;
  background: transparent;
}

.tag-pill-button {
  border: 1px solid transparent;
  border-radius: 0;
  padding: 6px 11px;
  background: var(--accent-soft);
  color: var(--accent);
  cursor: pointer;
}

.tag-pill-button:hover {
  background: rgba(139, 77, 44, 0.2);
}

.tag-pill-button.active {
  border-color: var(--border-strong);
  background: rgba(38, 75, 70, 0.14);
  color: var(--nav-active);
}

.exam-tag-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 0;
  background: rgba(255, 255, 255, 0.5);
}

.exam-tag-picker-hint {
  margin: 0 0 8px;
  font-size: 0.88rem;
}

.exam-tag-picker-custom-slot {
  display: contents;
}

.custom-tag-pill {
  gap: 8px;
  border: 1px solid var(--border-strong);
  background: rgba(38, 75, 70, 0.1);
  color: var(--nav-active);
}

.custom-tag-remove {
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
  padding: 0 0 0 4px;
  font-size: 1rem;
  line-height: 1;
}

.custom-tag-remove:hover {
  background: transparent;
  color: var(--accent-strong);
}

.exam-tag-picker-add {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  margin-top: 8px;
}

.exam-tag-picker-add .ghost-button {
  white-space: nowrap;
}

.auth-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.auth-tabs button.active {
  background: var(--nav-active);
}

.list {
  display: grid;
  gap: 12px;
}

.admin-grid {
  display: grid;
  gap: 18px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.admin-api-grid {
  display: grid;
  gap: 18px;
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  align-items: start;
}

.admin-card {
  margin-top: 0;
}

.admin-card-copy {
  margin-bottom: 12px;
}

.admin-api-form {
  display: grid;
  gap: 10px;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  margin-bottom: 16px;
}

.admin-api-form button {
  min-width: 144px;
}

.admin-section-error {
  margin-bottom: 16px;
}

.item {
  border: 1px solid var(--border);
  border-radius: 0;
  padding: 16px;
  background: rgba(255, 255, 255, 0.74);
}

.selected-item {
  border-color: rgba(38, 75, 70, 0.35);
  box-shadow: inset 0 0 0 1px rgba(38, 75, 70, 0.12);
}

.callout {
  padding: 18px 18px 12px;
  margin-bottom: 16px;
}

.utility-panel {
  padding: 0;
  overflow: hidden;
  margin-bottom: 20px;
}

.utility-panel summary {
  list-style: none;
  cursor: pointer;
  padding: 16px 18px;
  font-weight: 600;
  color: var(--accent);
  background: rgba(139, 77, 44, 0.06);
}

.utility-panel summary::-webkit-details-marker {
  display: none;
}

.utility-form,
.utility-copy {
  padding: 0 18px 18px;
}

.tag-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.course-progress-bar {
  display: grid;
  grid-template-columns: repeat(9, minmax(118px, 1fr));
  gap: 4px;
  margin: 24px 0 18px;
  overflow-x: auto;
  padding-bottom: 4px;
}

.course-progress-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.course-progress-segment {
  --progress: 0%;
  --segment-base: rgba(139, 77, 44, 0.12);
  --segment-fill: rgba(38, 75, 70, 0.42);
  position: relative;
  display: grid;
  grid-template-rows: minmax(46px, 1fr) auto;
  gap: 8px;
  min-height: 104px;
  min-width: 118px;
  padding: 12px;
  overflow: hidden;
  border: 1px solid rgba(112, 70, 38, 0.2);
  border-radius: 0;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.16), transparent),
    linear-gradient(90deg, var(--segment-fill) 0 var(--progress), var(--segment-base) var(--progress) 100%);
  color: var(--text);
  text-align: left;
}

.course-progress-segment:hover {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.22), transparent),
    linear-gradient(90deg, var(--segment-fill) 0 var(--progress), var(--segment-base) var(--progress) 100%);
}

.course-progress-segment.active {
  border-color: var(--nav-active);
  box-shadow: inset 0 0 0 2px rgba(38, 75, 70, 0.38);
}

.course-progress-segment.empty {
  --segment-base: rgba(139, 77, 44, 0.1);
  --segment-fill: rgba(139, 77, 44, 0.22);
}

.course-progress-segment.low {
  --segment-base: rgba(139, 77, 44, 0.13);
  --segment-fill: rgba(139, 77, 44, 0.32);
}

.course-progress-segment.medium {
  --segment-base: rgba(38, 75, 70, 0.12);
  --segment-fill: rgba(38, 75, 70, 0.42);
}

.course-progress-segment.high {
  --segment-base: rgba(38, 75, 70, 0.16);
  --segment-fill: rgba(38, 75, 70, 0.56);
}

.course-progress-segment.complete {
  --segment-base: rgba(31, 94, 61, 0.18);
  --segment-fill: rgba(31, 94, 61, 0.68);
}

.course-progress-segment-title {
  display: grid;
  gap: 3px;
  position: relative;
  z-index: 1;
  line-height: 1.16;
}

.course-progress-segment-title span {
  color: var(--muted);
  font-size: 0.78rem;
}

.course-progress-segment-title strong {
  font-size: 0.96rem;
}

.course-progress-segment-count {
  position: relative;
  z-index: 1;
  justify-self: start;
  padding: 4px 7px;
  border-radius: 0;
  background: rgba(255, 251, 244, 0.85);
  color: #3d2c1d;
  font-size: 0.82rem;
  font-weight: 600;
}
.inline-field {
  display: inline-grid;
  gap: 6px;
  align-items: center;
}

.compact-callout {
  padding: 14px 16px 10px;
}

.exam-focus {
  display: grid;
  gap: 16px;
  margin-bottom: 20px;
}

.exam-select-field {
  max-width: 420px;
}

.exam-focus-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.exam-progress-row {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) auto auto;
  gap: 10px;
  align-items: center;
  margin: 10px 0;
}

.mini-progress {
  height: 10px;
  overflow: hidden;
  border-radius: 0;
  background: rgba(139, 77, 44, 0.12);
}

.mini-progress span {
  display: block;
  width: var(--progress);
  height: 100%;
  border-radius: 0;
  background: var(--nav-active);
}

.study-shell,
.exam-cards-panel {
  margin-top: 16px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 0;
  background: rgba(255, 255, 255, 0.54);
}

.study-shell {
  padding: 24px 28px 28px;
}

.exam-cards-panel summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--accent);
}

.exam-cards-panel .card-table-wrap {
  margin-top: 14px;
}

.study-card {
  display: grid;
  gap: 22px;
}

.study-prompt {
  margin: 12px 0 4px;
  font-size: 1.55rem;
  line-height: 1.45;
}

.study-answer {
  padding: 16px 18px;
  border: 1px solid var(--border);
  border-radius: 0;
  background: var(--surface-strong);
  font-size: 1.1rem;
  line-height: 1.55;
}

.reveal-study-answer {
  justify-self: start;
  padding: 8px 16px;
  font-size: 0.95rem;
}

.study-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--muted);
  font-size: 0.92rem;
}

.study-actions {
  justify-content: flex-start;
}

.study-grade-button {
  min-width: 92px;
  padding: 9px 16px;
  background: rgba(139, 77, 44, 0.1);
  color: var(--accent);
  border: 1px solid var(--border-strong);
  font-size: 0.95rem;
  transition: background-color 120ms ease, color 120ms ease;
}

.study-grade-button:hover {
  background: rgba(139, 77, 44, 0.18);
  color: var(--accent-strong);
}

.study-grade-button[data-grade="WRONG"] {
  color: #8d2117;
  border-color: rgba(141, 33, 23, 0.36);
  background: rgba(141, 33, 23, 0.08);
}

.study-grade-button[data-grade="WRONG"]:hover {
  background: rgba(141, 33, 23, 0.16);
  color: #8d2117;
}

.study-grade-button[data-grade="RIGHT"] {
  color: #1f5e3d;
  border-color: rgba(31, 94, 61, 0.36);
  background: rgba(31, 94, 61, 0.08);
}

.study-grade-button[data-grade="RIGHT"]:hover {
  background: rgba(31, 94, 61, 0.16);
  color: #1f5e3d;
}

.study-grade-button[data-grade="EASY"] {
  color: var(--nav-active);
  border-color: rgba(38, 75, 70, 0.36);
  background: rgba(38, 75, 70, 0.08);
}

.study-grade-button[data-grade="EASY"]:hover {
  background: rgba(38, 75, 70, 0.16);
  color: var(--nav-active);
}

.code-block {
  margin: 8px 0 0;
  padding: 12px 14px;
  border-radius: 0;
  border: 1px solid var(--border);
  background: rgba(39, 24, 15, 0.06);
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

.card-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 0;
  background: rgba(255, 255, 255, 0.68);
}

.card-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}

.card-table th,
.card-table td {
  padding: 14px 16px;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--border);
}

.card-table th {
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.card-table tbody tr:last-child td {
  border-bottom: 0;
}

.table-action-button {
  min-width: 76px;
}

.answer-reveal-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.answer-reveal-text {
  line-height: 1.5;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 11px;
  border-radius: 0;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.87rem;
}

.pill-count {
  color: var(--muted);
}

.muted {
  color: var(--muted);
}

.error,
.success {
  margin-bottom: 12px;
}

.error {
  color: var(--error);
}

.success {
  color: var(--success);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg-top: #1d1511;
    --bg-bottom: #14100d;
    --surface: rgba(38, 30, 24, 0.88);
    --surface-strong: rgba(49, 38, 31, 0.94);
    --border: rgba(214, 182, 156, 0.14);
    --border-strong: rgba(214, 182, 156, 0.28);
    --text: #f3e7d9;
    --muted: #cfb9a4;
    --accent: #ecb07c;
    --accent-strong: #d38f58;
    --accent-soft: rgba(236, 176, 124, 0.14);
    --success: #82d5ab;
    --error: #f39a90;
    --nav-active: #6d8d7f;
    --shadow: 0 28px 60px rgba(0, 0, 0, 0.34);
  }

  .item {
    background: rgba(59, 45, 36, 0.72);
  }

  .code-block {
    background: rgba(0, 0, 0, 0.2);
  }

  .card-table-wrap {
    background: rgba(59, 45, 36, 0.72);
  }

  .study-shell,
  .exam-cards-panel {
    background: rgba(59, 45, 36, 0.58);
  }

  .study-mastery-bar {
    background: rgba(214, 182, 156, 0.1);
    border-color: rgba(214, 182, 156, 0.22);
  }

  .study-mastery-bar::before {
    background: rgba(109, 141, 127, 0.55);
  }

  .study-grade-button {
    background: rgba(214, 182, 156, 0.08);
    color: var(--accent);
    border-color: rgba(214, 182, 156, 0.28);
  }

  .study-grade-button:hover {
    background: rgba(214, 182, 156, 0.16);
    color: var(--accent);
  }

  .study-grade-button[data-grade="WRONG"] {
    color: var(--error);
    border-color: rgba(243, 154, 144, 0.4);
    background: rgba(243, 154, 144, 0.1);
  }

  .study-grade-button[data-grade="WRONG"]:hover {
    background: rgba(243, 154, 144, 0.2);
    color: var(--error);
  }

  .study-grade-button[data-grade="RIGHT"] {
    color: var(--success);
    border-color: rgba(130, 213, 171, 0.4);
    background: rgba(130, 213, 171, 0.1);
  }

  .study-grade-button[data-grade="RIGHT"]:hover {
    background: rgba(130, 213, 171, 0.2);
    color: var(--success);
  }

  .study-grade-button[data-grade="EASY"] {
    color: var(--nav-active);
    border-color: rgba(109, 141, 127, 0.45);
    background: rgba(109, 141, 127, 0.12);
  }

  .study-grade-button[data-grade="EASY"]:hover {
    background: rgba(109, 141, 127, 0.22);
    color: var(--nav-active);
  }
}

@media (max-width: 980px) {
  .grid,
  .admin-grid,
  .exam-focus-grid {
    grid-template-columns: 1fr;
  }

  .admin-api-grid {
    grid-template-columns: 1fr;
  }

  .panel-wide {
    min-height: 0;
  }
}

@media (max-width: 640px) {
  .page-shell {
    padding: 20px 14px 44px;
  }

  .top-nav {
    border-radius: 0;
  }

  .panel {
    padding: 18px;
  }

  .panel-header,
  .item-header,
  .button-row {
    align-items: stretch;
    flex-direction: column;
  }

  .exam-form-grid {
    grid-template-columns: 1fr;
  }

  .button-row button,
  .compact-row button,
  .ghost-link {
    width: 100%;
  }

  .exam-progress-row {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .admin-api-form {
    grid-template-columns: 1fr;
  }

  .admin-api-form button {
    width: 100%;
  }
}

.apkg-note {
  border: 1px solid var(--theme-divider, #d8d8e0);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 8px;
  display: grid;
  gap: 6px;
}

.apkg-note-added {
  opacity: 0.6;
}

.apkg-note-prompt {
  font-size: 1.02em;
}

.apkg-note-answer {
  min-height: 1.4em;
}

.apkg-note-tags,
.apkg-note-color {
  font-size: 0.9em;
}

.apkg-note-actions {
  margin-top: 4px;
}
