:root {
  --bg: #d9d9d9;
  --panel: #ffffff;
  --surface: #ffffff;
  --surface-2: #ececec;
  --surface-3: #e6e6e6;
  --input-bg: #ffffff;
  --border: #999999;
  --border-light: #c8c8c8;
  --text: #000000;
  --muted: #444444;
  --accent: #003399;
  --accent-strong: #002a80;
  --good: #2e7d32;
  --good-bg: #d7f0d2;
  --bad: #a31515;
  --bad-bg: #f8d7d7;
  --warn: #a36a00;
  --explain-bg: #ffffcc;
  --explain-border: #d8cf7a;
  --option-hover-bg: #eef4ff;
  --option-hover-border: #99b8e8;
  --selected-bg: #e6edfa;
  --dim-bg: #f0f0f0;
  --revealed-bg: #dbe8ff;
  --btn-top: #ffffff;
  --btn-bottom: #e2e2e2;
  --btn-hover-top: #f5f5f5;
  --btn-hover-bottom: #d5d5d5;
  --btn-active: #d0d0d0;
  --primary-top: #dceaff;
  --primary-bottom: #b9d1f5;
  --primary-border: #7ea6dd;
  --primary-hover-top: #cfe1fb;
  --primary-hover-bottom: #a9c6ee;
  --shadow: rgba(0,0,0,.25);
  --radius: 2px;
}

:root[data-theme="dark"] {
  --bg: #17191b;
  --panel: #232629;
  --surface: #232629;
  --surface-2: #2a2e32;
  --surface-3: #34393e;
  --input-bg: #2a2e32;
  --border: #55595e;
  --border-light: #3d4247;
  --text: #e8e8e8;
  --muted: #aaaaaa;
  --accent: #6ea8e0;
  --accent-strong: #8fbdea;
  --good: #7ed08a;
  --good-bg: #1f3a22;
  --bad: #ff8a8a;
  --bad-bg: #3f1f1f;
  --warn: #e3b458;
  --explain-bg: #3a3520;
  --explain-border: #5c5430;
  --option-hover-bg: #22364a;
  --option-hover-border: #4a76a8;
  --selected-bg: #203a54;
  --dim-bg: #2a2d30;
  --revealed-bg: #263a54;
  --btn-top: #34393e;
  --btn-bottom: #2a2e32;
  --btn-hover-top: #3c4247;
  --btn-hover-bottom: #2f3438;
  --btn-active: #24282b;
  --primary-top: #2c4a68;
  --primary-bottom: #1f3a54;
  --primary-border: #4a769e;
  --primary-hover-top: #345676;
  --primary-hover-bottom: #264260;
  --shadow: rgba(0,0,0,.5);
}

html { font-size: 16px; }

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-size: 0.8125rem;
}

.topbar {
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}

.topbar-row {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 16px;
  flex-wrap: wrap;
}

.topbar-row h1 { font-size: 0.9375rem; font-weight: bold; margin: 0; white-space: nowrap; color: var(--text); }

#examSelect {
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 6px;
  font-size: 0.8125rem;
  font-family: Arial, Helvetica, sans-serif;
  flex: 1;
  min-width: 150px;
  max-width: 300px;
}

.learn-toggle {
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 12px;
  font-size: 0.75rem;
  font-weight: bold;
  font-family: Arial, Helvetica, sans-serif;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.learn-toggle:hover { background: var(--surface-3); }
.learn-toggle.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.learn-toggle.active:hover { background: var(--accent-strong); }

.theme-toggle {
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 30px;
  height: 30px;
  padding: 0;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.theme-toggle:hover { background: var(--surface-3); }

.stats { display: flex; gap: 8px; color: var(--text); font-size: 0.75rem; margin-left: auto; }
.stats span { background: var(--input-bg); padding: 3px 9px; border: 1px solid var(--border); white-space: nowrap; }

.progress-bar {
  height: 4px;
  background: var(--border-light);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width .2s;
}

.palette-toggle {
  margin: 10px auto 0;
  display: block;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 5px 14px;
  font-size: 0.75rem;
  font-family: Arial, Helvetica, sans-serif;
  cursor: pointer;
}
.palette-toggle:hover { background: var(--surface-3); }

.palette[hidden] { display: none; }

.palette {
  max-width: 760px;
  margin: 10px auto 0;
  padding: 12px 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(36px, 1fr));
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.palette-item {
  position: relative;
  height: 32px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.75rem;
  font-family: Arial, Helvetica, sans-serif;
  cursor: pointer;
}
.palette-item:hover { background: var(--surface-3); }
.palette-item.current { border-color: var(--accent); border-width: 2px; }
.palette-item.correct { background: var(--good-bg); border-color: var(--good); }
.palette-item.incorrect { background: var(--bad-bg); border-color: var(--bad); }
.palette-item.revealed { background: var(--revealed-bg); border-color: var(--accent); }
.palette-item.marked::after {
  content: '';
  position: absolute;
  top: -3px; right: -3px;
  width: 9px; height: 9px;
  border-radius: 50%;
  background: var(--warn);
  border: 1px solid var(--surface);
}

main#app {
  flex: 1;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  padding: 16px 16px 130px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}

.context {
  color: var(--text);
  font-size: 0.8125rem;
  line-height: 1.5;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-light);
}
.context p, .prompt p, .feedback p { margin: 0 0 6px; }
.context p:last-child, .prompt p:last-child, .feedback p:last-child { margin-bottom: 0; }

.prompt {
  font-size: 0.875rem;
  font-weight: bold;
  margin-bottom: 14px;
  line-height: 1.45;
  color: var(--text);
}

.badge-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }

.badge {
  display: inline-block;
  font-size: 0.6875rem;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: .02em;
  color: var(--accent);
}

.badge.marked { color: var(--warn); }
.badge.marked::before { content: '\2605  '; }

.screenshot-btn {
  margin-left: auto;
  padding: 4px 10px;
  min-height: 0;
  font-size: 0.6875rem;
  font-weight: bold;
}

.options { display: flex; flex-direction: column; gap: 6px; }

.option {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 9px 10px;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  font-size: 0.8125rem;
  line-height: 1.4;
  min-height: 40px;
}

.option:hover { background: var(--option-hover-bg); border-color: var(--option-hover-border); }
.option input { pointer-events: none; margin-top: 2px; flex-shrink: 0; }
.option .opt-letter { font-weight: bold; margin-right: 2px; }
.option.selected { border-color: var(--accent); background: var(--selected-bg); }
.option.correct { border-color: var(--good); background: var(--good-bg); }
.option.incorrect { border-color: var(--bad); background: var(--bad-bg); }
.option.dim { opacity: .5; background: var(--dim-bg); border-color: var(--border-light); }
.option.disabled { cursor: default; }

.blank-row {
  margin-bottom: 14px;
}
.blank-row label {
  display: block;
  font-size: 0.75rem;
  font-weight: bold;
  color: var(--text);
  margin-bottom: 4px;
}
.blank-row select {
  width: 100%;
  padding: 8px;
  font-size: 0.8125rem;
  font-family: Arial, Helvetica, sans-serif;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-height: 38px;
}
.blank-row select.correct { border-color: var(--good); background: var(--good-bg); }
.blank-row select.incorrect { border-color: var(--bad); background: var(--bad-bg); }

.drag-wrap { display: flex; gap: 16px; flex-wrap: wrap; }
.drag-choices, .drag-targets { flex: 1; min-width: 220px; display: flex; flex-direction: column; gap: 6px; }
.drag-section-title { font-size: 0.6875rem; font-weight: bold; color: var(--text); text-transform: uppercase; letter-spacing: .02em; margin-bottom: 2px; }

.chip {
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  cursor: grab;
  font-size: 0.8125rem;
  user-select: none;
  min-height: 38px;
  display: flex;
  align-items: center;
}
.chip.used { opacity: .4; cursor: default; }
.chip.picked { border-color: var(--accent); background: var(--selected-bg); }

.target {
  min-height: 40px;
  padding: 8px 10px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: 0.8125rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  cursor: pointer;
}
.target .target-label { color: var(--muted); font-size: 0.6875rem; font-weight: bold; min-width: 40%; }
.target.filled { border-style: solid; background: var(--surface); }
.target.correct { border-color: var(--good); background: var(--good-bg); }
.target.incorrect { border-color: var(--bad); background: var(--bad-bg); }
.target.dragover { border-color: var(--accent); }

.feedback {
  margin-top: 14px;
  padding: 10px 12px;
  border-radius: var(--radius);
  font-size: 0.8125rem;
  line-height: 1.5;
  background: var(--explain-bg);
  border: 1px solid var(--explain-border);
  color: #1a1a1a;
}
:root[data-theme="dark"] .feedback { color: var(--text); }
.feedback .verdict { font-weight: bold; margin-bottom: 6px; display: block; }
.feedback.good .verdict { color: var(--good); }
.feedback.bad .verdict { color: var(--bad); }
.feedback.info .verdict { color: var(--accent); }

.hint { color: var(--muted); font-size: 0.75rem; margin-top: 8px; font-style: italic; }

footer.controls {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface-2);
  border-top: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  z-index: 6;
}

button {
  padding: 8px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: linear-gradient(var(--btn-top), var(--btn-bottom));
  color: var(--text);
  font-size: 0.8125rem;
  font-family: Arial, Helvetica, sans-serif;
  cursor: pointer;
  min-height: 36px;
}
button:hover:not(:disabled) { background: linear-gradient(var(--btn-hover-top), var(--btn-hover-bottom)); }
button:active:not(:disabled) { background: var(--btn-active); }
button.primary { background: linear-gradient(var(--primary-top), var(--primary-bottom)); border-color: var(--primary-border); font-weight: bold; }
button.primary:hover:not(:disabled) { background: linear-gradient(var(--primary-hover-top), var(--primary-hover-bottom)); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.marked-on { border-color: var(--warn); color: var(--warn); font-weight: bold; }

.summary { text-align: center; padding: 36px 20px; }
.summary .big { font-size: 2.375rem; font-weight: bold; margin: 10px 0; color: var(--text); }
.summary .sub { color: var(--muted); margin-bottom: 8px; }
.summary .breakdown { color: var(--muted); font-size: 0.8125rem; margin-top: 8px; }

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 300px;
  max-width: calc(100vw - 32px);
  text-align: center;
}
.login-card h1 { font-size: 0.9375rem; font-weight: bold; margin: 0 0 18px; color: var(--text); }
.login-card input {
  width: 100%;
  padding: 9px 8px;
  font-size: 0.9375rem;
  font-family: Arial, Helvetica, sans-serif;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
  min-height: 38px;
}
.login-card button {
  width: 100%;
  background: linear-gradient(var(--primary-top), var(--primary-bottom));
  border-color: var(--primary-border);
  font-weight: bold;
}
.login-error { color: var(--bad); font-size: 0.75rem; margin-bottom: 10px; }

.font-ctrl {
  position: fixed;
  right: 10px;
  bottom: 66px;
  z-index: 20;
  display: flex;
  gap: 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 3px;
  box-shadow: 0 1px 4px var(--shadow);
}
.font-ctrl button {
  min-height: 30px;
  min-width: 34px;
  padding: 4px 8px;
  font-weight: bold;
  font-size: 0.8125rem;
}

@media (max-width: 640px) {
  footer.controls {
    position: static;
    justify-content: flex-start;
  }
  main#app { padding-bottom: 20px; }
  .topbar-row { padding: 8px 10px; gap: 8px; }
  .topbar-row h1 { font-size: 0.875rem; }
  #examSelect { min-width: 110px; }
  .stats span { padding: 3px 6px; }
  .card { padding: 14px; }
  footer.controls button { flex: 1 1 auto; min-width: 0; }
  .font-ctrl {
    position: static;
    width: fit-content;
    margin: 10px auto 16px;
    box-shadow: none;
  }
}

@media (max-width: 480px) {
  .drag-wrap { flex-direction: column; }
  .stats { margin-left: 0; }
}

.image-modal[hidden] { display: none; }
.image-modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.image-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.6);
}
.image-modal-content {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-width: 96vw;
  width: 100%;
  max-height: 94vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 4px 20px var(--shadow);
}
.image-modal-content:fullscreen {
  max-width: 100vw;
  max-height: 100vh;
  width: 100vw;
  height: 100vh;
  border: none;
  border-radius: 0;
}
.image-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-weight: bold;
  font-size: 0.8125rem;
  color: var(--text);
  gap: 10px;
}
.image-modal-hint { font-weight: normal; color: var(--muted); font-size: 0.75rem; }
.image-modal-actions { display: flex; gap: 6px; flex-shrink: 0; }
.image-modal-close {
  min-height: 28px;
  min-width: 28px;
  padding: 0;
  font-size: 0.8125rem;
}
.image-modal-body {
  overflow: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  flex: 1;
}
.image-modal-body img {
  width: 100%;
  height: auto;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  display: block;
  cursor: zoom-in;
}
.image-modal-body img.zoomed {
  width: auto;
  max-width: none;
  align-self: flex-start;
  cursor: zoom-out;
}
