/* ─── Design tokens ──────────────────────────────────────────────────────── */
:root {
  /* Dark (default) */
  --bg:            #080e17;
  --bg2:           #0d1520;
  --surface:       #111c2b;
  --surface2:      #162233;
  --surface3:      #1c2d3f;
  --border:        #1f3048;
  --border2:       #2a405c;
  --ink:           #e8f0f8;
  --ink2:          #b4c4d8;
  --muted:         #6a82a0;
  --brand:         #38bdf8;
  --brand-dim:     #0ea5e9;
  --brand-glow:    rgba(56, 189, 248, 0.15);
  --accent:        #f59e0b;
  --accent-dim:    rgba(245, 158, 11, 0.18);
  --ok:            #34d399;
  --ok-dim:        rgba(52, 211, 153, 0.14);
  --bad:           #f87171;
  --bad-dim:       rgba(248, 113, 113, 0.15);
  --warn:          #fbbf24;
  --focus-ring:    rgba(56, 189, 248, 0.55);
  --shadow-sm:     0 1px 3px rgba(0,0,0,0.4), 0 1px 2px rgba(0,0,0,0.3);
  --shadow:        0 4px 16px rgba(0,0,0,0.45), 0 1px 4px rgba(0,0,0,0.3);
  --shadow-lg:     0 12px 40px rgba(0,0,0,0.55), 0 2px 8px rgba(0,0,0,0.35);
  --radius-sm:     6px;
  --radius:        10px;
  --radius-lg:     14px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

:root[data-theme="light"] {
  --bg:            #f0f4f8;
  --bg2:           #e6edf5;
  --surface:       #ffffff;
  --surface2:      #f7f9fb;
  --surface3:      #edf2f7;
  --border:        #d1dce8;
  --border2:       #bccad8;
  --ink:           #0f1e2e;
  --ink2:          #2d4460;
  --muted:         #64809a;
  --brand:         #0284c7;
  --brand-dim:     #0369a1;
  --brand-glow:    rgba(2, 132, 199, 0.12);
  --accent:        #d97706;
  --accent-dim:    rgba(217, 119, 6, 0.12);
  --ok:            #059669;
  --ok-dim:        rgba(5, 150, 105, 0.10);
  --bad:           #dc2626;
  --bad-dim:       rgba(220, 38, 38, 0.10);
  --warn:          #d97706;
  --focus-ring:    rgba(2, 132, 199, 0.45);
  --shadow-sm:     0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.05);
  --shadow:        0 4px 16px rgba(0,0,0,0.10), 0 1px 4px rgba(0,0,0,0.07);
  --shadow-lg:     0 12px 40px rgba(0,0,0,0.13), 0 2px 8px rgba(0,0,0,0.08);
}

/* ─── Reset ──────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
button, input, textarea, select { font: inherit; }
button { border: 0; cursor: pointer; background: none; }
h1, h2, h3, p { margin: 0; }

/* ─── Base ───────────────────────────────────────────────────────────────── */
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--bg);
  background-image:
    radial-gradient(ellipse 80% 50% at 10% -10%, rgba(56,189,248,0.07) 0%, transparent 60%),
    radial-gradient(ellipse 60% 40% at 90% 110%, rgba(245,158,11,0.04) 0%, transparent 55%);
}

:root[data-theme="light"] body {
  background: var(--bg);
  background-image:
    radial-gradient(ellipse 80% 50% at 10% -10%, rgba(2,132,199,0.06) 0%, transparent 60%);
}

/* ─── Focus ──────────────────────────────────────────────────────────────── */
:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}

/* ─── Scrollbar ──────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ─── Buttons ────────────────────────────────────────────────────────────── */
.primary-button,
.ghost-button,
.danger-button,
.mode-button,
.tab-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  font-size: 0.84rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  white-space: nowrap;
  transition: background 120ms, color 120ms, border-color 120ms, box-shadow 120ms, transform 100ms;
  user-select: none;
}

.primary-button {
  color: #fff;
  background: var(--brand);
  box-shadow: 0 0 0 1px var(--brand-dim), var(--shadow-sm);
}
.primary-button:hover:not(:disabled) {
  background: var(--brand-dim);
  transform: translateY(-1px);
  box-shadow: 0 0 0 1px var(--brand-dim), var(--shadow);
}

.ghost-button, .mode-button, .tab-button {
  color: var(--ink2);
  background: var(--surface2);
  border: 1px solid var(--border);
}
.ghost-button:hover:not(:disabled),
.mode-button:hover:not(:disabled),
.tab-button:hover:not(:disabled) {
  color: var(--ink);
  border-color: var(--border2);
  background: var(--surface3);
}

.danger-button {
  color: #fff;
  background: var(--bad);
  box-shadow: 0 0 0 1px rgba(248,113,113,0.4), var(--shadow-sm);
}
.danger-button:hover:not(:disabled) {
  filter: brightness(1.12);
  transform: translateY(-1px);
}

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

.mode-button.active, .tab-button.active {
  color: var(--brand);
  background: var(--brand-glow);
  border-color: rgba(56, 189, 248, 0.35);
  box-shadow: inset 0 0 0 1px rgba(56,189,248,0.18);
}
:root[data-theme="light"] .mode-button.active,
:root[data-theme="light"] .tab-button.active {
  color: var(--brand);
  background: var(--brand-glow);
  border-color: rgba(2,132,199,0.3);
}

.mini-button {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  padding: 0 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--muted);
  background: var(--surface2);
  font-size: 0.75rem;
  font-weight: 700;
  transition: background 120ms, color 120ms, border-color 120ms;
}
.mini-button.active {
  color: var(--brand);
  background: var(--brand-glow);
  border-color: rgba(56,189,248,0.35);
}

/* ─── Layout ─────────────────────────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px clamp(16px, 3.5vw, 40px) 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 0;
  z-index: 10;
}

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

.app-header-brand .logo-mark {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--brand-glow);
  border: 1px solid rgba(56,189,248,0.3);
  display: grid;
  place-items: center;
  font-size: 1rem;
}

.app-header-brand .brand-text {
  min-width: 0;
}

.eyebrow {
  color: var(--brand);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.10em;
  text-transform: uppercase;
  line-height: 1;
  margin-bottom: 2px;
}

.app-header h1 {
  font-size: clamp(0.95rem, 2vw, 1.15rem);
  font-weight: 700;
  color: var(--ink);
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.app-shell {
  display: grid;
  grid-template-columns: 292px minmax(0, 1fr) 300px;
  gap: 0;
  min-height: calc(100vh - 65px);
}

/* ─── Panels ─────────────────────────────────────────────────────────────── */
.lesson-panel {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.editor-panel {
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.workspace {
  padding: clamp(16px, 2.5vw, 28px);
  background: var(--bg2);
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
}

/* ─── Left panel internals ───────────────────────────────────────────────── */
.day-planner {
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--border);
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.day-grid {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 330px;
  overflow-y: auto;
  padding-right: 2px;
}

.day-card {
  display: grid;
  grid-template-columns: 56px 1fr 54px;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface2);
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  transition: border-color 120ms, background 120ms;
  min-width: 0;
  min-height: 42px;
}
.day-card:hover {
  border-color: var(--border2);
  background: var(--surface3);
}
.day-card.active {
  border-color: var(--brand);
  background: var(--brand-glow);
}
.day-card strong {
  font-size: 0.78rem;
  font-weight: 800;
  color: var(--ink2);
  line-height: 1.15;
  white-space: nowrap;
}
.day-card .slot-row {
  display: flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
}
.day-card .slot-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 42px;
  height: 22px;
  border-radius: 999px;
  border: 1px solid var(--border2);
  color: var(--muted);
  background: var(--surface);
  font-size: 0.72rem;
  font-weight: 800;
}
.day-card .slot-chip.filled {
  color: #052014;
  background: var(--ok);
  border-color: var(--ok);
}
.day-card small {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  line-height: 1.1;
  text-align: right;
  white-space: nowrap;
}

/* Lesson search + list */
.panel-top {
  padding: 10px 14px 0;
}

.search-box {
  display: flex;
  flex-direction: column;
  gap: 5px;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 700;
}

.lesson-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
}

.lesson-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  padding: 9px 11px;
  text-align: left;
  color: var(--ink);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 100ms, border-color 100ms;
}
.lesson-item:hover {
  background: var(--surface2);
  border-color: var(--border);
}
.lesson-item.active {
  background: var(--brand-glow);
  border-color: rgba(56,189,248,0.28);
}
.lesson-item strong {
  font-size: 0.84rem;
  font-weight: 700;
  line-height: 1.3;
  color: var(--ink);
}
.lesson-item span {
  font-size: 0.74rem;
  color: var(--muted);
  line-height: 1.2;
}

/* ─── Workspace ──────────────────────────────────────────────────────────── */
.workspace-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.workspace-top h2 {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--ink);
  line-height: 1.3;
}

.workspace-top .eyebrow {
  margin-bottom: 3px;
}

.mode-group {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

/* Stats strip */
.stats-strip {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.stats-strip div {
  display: flex;
  flex-direction: column;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--border);
}

.stats-strip span {
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--ink);
  line-height: 1.1;
}

.stats-strip small {
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 1px;
}

/* Practice controls */
.practice-controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  flex-wrap: wrap;
}

.hint-control {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.hint-control select {
  width: auto;
  min-width: 80px;
}

/* Next-key hint */
.next-key {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px 7px 12px;
  border: 1px solid rgba(245, 158, 11, 0.3);
  border-radius: var(--radius-sm);
  background: var(--accent-dim);
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
  width: fit-content;
  max-width: 100%;
}

.next-key strong {
  min-width: 44px;
  padding: 3px 8px;
  border-radius: 5px;
  color: #0f1e2e;
  text-align: center;
  background: var(--accent);
  font-size: 0.88rem;
  font-weight: 800;
  letter-spacing: 0.02em;
}

/* ─── Typing stage ───────────────────────────────────────────────────────── */
.typing-stage {
  position: relative;
  flex: 1;
  min-height: 320px;
  height: min(52vh, 580px);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: text;
  box-shadow: var(--shadow-sm), inset 0 1px 0 rgba(255,255,255,0.03);
  transition: border-color 150ms, box-shadow 150ms;
}

/* IME-safe capture field: invisible but focusable so the Vietnamese input
   method delivers composed characters via input/beforeinput events. */
.typing-capture {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  margin: 0;
  opacity: 0;
  resize: none;
  overflow: hidden;
  background: transparent;
  color: transparent;
  caret-color: transparent;
  pointer-events: none;
}
.typing-capture:focus-visible { outline: none; }

.typing-stage:focus-visible,
.typing-stage:focus-within {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--focus-ring), var(--shadow);
  outline: none;
}

.typing-text {
  padding: clamp(16px, 2.5vw, 28px);
  font-family: "Cascadia Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 1.05rem;
  line-height: 2;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  letter-spacing: 0.01em;
}

/* Characters */
.char {
  position: relative;
  border-radius: 3px;
  color: var(--muted);
  transition: color 60ms;
}

.char.correct {
  color: var(--ink2);
  background: var(--ok-dim);
}

.char.current {
  color: var(--ink);
  background: var(--accent-dim);
  box-shadow: inset 0 -2px 0 var(--accent);
}

.char.wrong {
  color: #fff;
  background: var(--bad);
  animation: shake 120ms ease;
}

.char.masked {
  color: var(--border2);
  background: var(--surface2);
}

.char.space {
  color: var(--border2);
}

.char.linebreak {
  color: var(--accent);
  font-weight: 800;
  padding: 0 4px;
  border: 1px solid rgba(245,158,11,0.3);
  border-radius: 3px;
}

@keyframes shake {
  0%   { transform: translateX(-3px); }
  50%  { transform: translateX(3px); }
  100% { transform: translateX(0); }
}

/* Stage help text */
.stage-help {
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.5;
  min-height: 20px;
}
.stage-help.warn {
  color: var(--warn);
  font-weight: 700;
}

/* ─── Editor panel ───────────────────────────────────────────────────────── */
.editor-tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  padding: 0 12px;
}

.tab-button {
  border-radius: 0;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--muted);
  padding: 0 10px;
  min-height: 44px;
  font-size: 0.82rem;
  margin-bottom: -1px;
  transition: color 120ms, border-color 120ms;
}
.tab-button:hover:not(:disabled) {
  color: var(--ink);
  background: transparent;
  transform: none;
  border-color: var(--border2);
}
.tab-button.active {
  color: var(--brand);
  border-bottom-color: var(--brand);
  background: transparent;
  box-shadow: none;
}

.tab-page {
  display: none;
}
.tab-page.active {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  flex: 1;
  overflow-y: auto;
}

/* Form fields */
.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.field > span,
.search-box > span,
.hint-control > span {
  font-size: 0.74rem;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

input, textarea, select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--ink);
  background: var(--surface2);
  transition: border-color 120ms, box-shadow 120ms;
}
input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

input, select {
  min-height: 36px;
  padding: 0 10px;
  font-size: 0.84rem;
}

textarea {
  resize: vertical;
  padding: 10px;
  line-height: 1.55;
  font-size: 0.84rem;
  min-height: 220px;
}

/* Writing metrics pills */
.writing-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.metric-pill {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
}
.metric-pill span {
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.metric-pill strong {
  font-size: 0.92rem;
  font-weight: 800;
  color: var(--ink);
}
.metric-pill.good { border-color: rgba(52, 211, 153, 0.4); }
.metric-pill.warn { border-color: rgba(251, 191, 36, 0.5); }
.metric-pill.bad  { border-color: rgba(248, 113, 113, 0.45); }

/* Button rows */
.button-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.button-row.vertical {
  flex-direction: column;
  align-items: stretch;
}
.button-row.end { justify-content: flex-end; }

/* Data tab */
.data-note {
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.55;
  padding: 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.status-box {
  min-height: 40px;
  padding: 10px;
  border-radius: var(--radius-sm);
  background: var(--surface2);
  border: 1px solid var(--border);
  font-size: 0.82rem;
  color: var(--muted);
  line-height: 1.5;
}

/* ─── Dialog ─────────────────────────────────────────────────────────────── */
.confirm-dialog {
  width: min(400px, calc(100vw - 24px));
  border: 1px solid var(--border2);
  border-radius: var(--radius-lg);
  padding: 0;
  box-shadow: var(--shadow-lg);
  background: var(--surface);
  color: var(--ink);
}

.confirm-dialog form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 24px;
}

.confirm-dialog h3 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--ink);
}

.confirm-dialog p {
  font-size: 0.86rem;
  color: var(--ink2);
  line-height: 1.55;
}

.confirm-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
}

/* ─── Responsive ─────────────────────────────────────────────────────────── */
@media (max-width: 1100px) {
  .app-shell {
    grid-template-columns: 280px minmax(0, 1fr);
    grid-template-rows: auto auto;
  }
  .editor-panel {
    grid-column: 1 / -1;
    border-left: none;
    border-top: 1px solid var(--border);
  }
  .tab-page.active {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
  }
  .tab-page.active > * { flex: 1 1 220px; }
  .tab-page.active .button-row { flex: 0 0 100%; }
}

@media (max-width: 720px) {
  .app-header h1 { display: none; }

  .app-shell {
    grid-template-columns: 1fr;
  }

  .lesson-panel {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 44vh;
  }

  .editor-panel {
    border-left: none;
    border-top: 1px solid var(--border);
  }

  .day-grid {
    max-height: 260px;
  }

  .stats-strip {
    grid-template-columns: repeat(3, 1fr);
  }

  .stats-strip div:nth-child(4),
  .stats-strip div:nth-child(5) {
    display: none;
  }

  .typing-stage {
    min-height: 260px;
    height: 40vh;
  }

  .tab-page.active {
    flex-direction: column;
  }
  .tab-page.active > * { flex: none; }

  .primary-button, .ghost-button, .danger-button, .mode-button {
    flex: 1 1 auto;
  }
}
