:root {
  --bg: #0f1320;
  --bg-2: #161b2e;
  --panel: #1c2238;
  --panel-2: #232a44;
  --text: #e8ecf7;
  --muted: #8a93b3;
  --accent: #6c8cff;
  --accent-2: #8c6cff;
  --good: #4ade80;
  --bad: #f87171;
  --warn: #fbbf24;
  --border: #2a3252;
  --radius: 12px;
  --shadow: 0 6px 20px rgba(0,0,0,0.25);
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  background: linear-gradient(180deg, #0b0f1c 0%, #0f1320 100%);
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: 15px;
  min-height: 100vh;
}
h1 { font-size: 1.6em; margin: 0 0 6px; }
h2 { font-size: 1.15em; margin: 28px 0 10px; color: #cbd3ee; }
.muted { color: var(--muted); }
.small { font-size: 0.85em; }
.hidden { display: none !important; }

/* Topbar */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  background: rgba(15,19,32,0.7);
  backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-size: 1.1em; }
.tabs { display: flex; gap: 4px; margin-left: 12px; flex: 1; }
.tab {
  background: transparent; border: 0; color: var(--muted);
  padding: 8px 14px; border-radius: 8px; cursor: pointer;
  font-size: 0.95em;
}
.tab:hover { background: var(--panel); color: var(--text); }
.tab.active { background: var(--panel-2); color: var(--text); }
.streak {
  background: var(--panel); padding: 6px 12px;
  border-radius: 999px; border: 1px solid var(--border);
  font-weight: 600;
}

main { max-width: 1100px; margin: 0 auto; padding: 24px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Dashboard */
.dash-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px; margin: 18px 0;
}
.stat-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
  text-align: center;
}
.stat-num { font-size: 1.8em; font-weight: 700; color: var(--accent); }
.stat-label { color: var(--muted); font-size: 0.85em; margin-top: 4px; }

.mode-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}
.mode-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px; cursor: pointer;
  color: var(--text); text-align: left;
  transition: transform 0.15s, border-color 0.15s, background 0.15s;
}
.mode-card:hover { transform: translateY(-2px); border-color: var(--accent); background: var(--panel-2); }
.mode-icon { font-size: 1.8em; margin-bottom: 6px; }
.mode-name { font-weight: 600; margin-bottom: 2px; }
.mode-desc { color: var(--muted); font-size: 0.85em; }

.session-config {
  display: flex; gap: 12px; flex-wrap: wrap;
  margin-top: 16px; padding: 14px;
  background: var(--panel); border-radius: var(--radius);
  border: 1px solid var(--border);
}
.session-config label { display: flex; flex-direction: column; gap: 4px; font-size: 0.85em; color: var(--muted); }

/* Inputs */
input, select, textarea, button {
  font-family: inherit; font-size: inherit;
}
input[type=text], input[type=search], input[type=password], input[type=number],
select, textarea {
  background: var(--bg-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

button {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 14px; cursor: pointer;
}
button:hover { background: var(--panel); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
button.ghost { background: transparent; }
button.danger { background: #3a1e26; border-color: #5a2b35; color: #fca5a5; }
button.danger:hover { background: #4d2530; }
button:disabled { opacity: 0.5; cursor: not-allowed; }

/* Study */
.study-header {
  display: flex; align-items: center; gap: 14px; margin-bottom: 24px;
}
.progress-bar { flex: 1; height: 8px; background: var(--panel); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); width: 0; transition: width 0.3s; }

#study-area { min-height: 360px; }

.card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 32px;
  box-shadow: var(--shadow);
  max-width: 640px; margin: 0 auto;
}
.card-prompt { font-size: 0.85em; color: var(--muted); margin-bottom: 8px; }
.card-word { font-size: 2.4em; font-weight: 700; text-align: center; margin: 16px 0; }
.card-ipa { text-align: center; color: var(--muted); font-style: italic; margin-bottom: 8px; }
.card-pos { text-align: center; color: var(--accent); font-size: 0.85em; margin-bottom: 12px; }
.card-meaning { font-size: 1.4em; text-align: center; color: var(--good); margin: 12px 0; }
.card-example { font-style: italic; color: var(--muted); text-align: center; margin-top: 16px; padding: 12px; background: var(--bg-2); border-radius: 8px; }
.card-actions { display: flex; gap: 8px; justify-content: center; margin-top: 24px; flex-wrap: wrap; }
.card-actions button { min-width: 100px; }

.rate-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 20px;
}
.rate-btn { display: flex; flex-direction: column; gap: 2px; padding: 12px; }
.rate-btn b { font-size: 1.05em; }
.rate-btn .interval { font-size: 0.8em; color: var(--muted); }
.rate-again { background: #3a1e26; border-color: #5a2b35; }
.rate-hard { background: #3a2e1e; border-color: #5a4b2b; }
.rate-good { background: #1e3a2c; border-color: #2b5a3f; }
.rate-easy { background: #1e2e3a; border-color: #2b4a5a; }

.mcq-options { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }
.mcq-opt { padding: 14px; text-align: left; }
.mcq-opt.correct { background: #1e3a2c; border-color: var(--good); }
.mcq-opt.wrong { background: #3a1e26; border-color: var(--bad); }

.input-row { display: flex; gap: 8px; margin-top: 16px; justify-content: center; }
.input-row input { flex: 1; max-width: 320px; font-size: 1.1em; }
.feedback { text-align: center; margin-top: 12px; font-size: 1.1em; }
.feedback.good { color: var(--good); }
.feedback.bad { color: var(--bad); }

.match-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
  max-width: 720px; margin: 0 auto;
}
.match-cell {
  padding: 16px; min-height: 64px;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 8px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  text-align: center; font-size: 0.95em;
  transition: all 0.15s;
}
.match-cell.selected { border-color: var(--accent); background: var(--bg-2); }
.match-cell.matched { background: #1e3a2c; border-color: var(--good); opacity: 0.6; cursor: default; position: relative; }
.match-cell.matched[data-pair]::before {
  content: attr(data-pair);
  position: absolute;
  top: 4px; left: 6px;
  width: 22px; height: 22px;
  display: flex; align-items: center; justify-content: center;
  background: var(--good);
  color: #0b0f1c;
  font-weight: 700;
  font-size: 0.85em;
  border-radius: 50%;
  opacity: 1;
}
.match-cell.shake { animation: shake 0.3s; }
@keyframes shake { 0%,100%{transform:translateX(0)} 25%{transform:translateX(-6px)} 75%{transform:translateX(6px)} }

#study-summary { text-align: center; padding: 40px; }
.summary-stats { display: flex; justify-content: center; gap: 32px; margin: 20px 0; font-size: 1.2em; }

/* Words table */
.words-toolbar {
  display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px;
}
.words-toolbar input[type=search] { flex: 1; min-width: 200px; }

.words-table {
  width: 100%; border-collapse: collapse;
  background: var(--panel); border-radius: var(--radius); overflow: hidden;
}
.words-table th, .words-table td {
  padding: 10px 12px; text-align: left;
  border-bottom: 1px solid var(--border);
}
.words-table th { background: var(--panel-2); color: var(--muted); font-size: 0.85em; font-weight: 600; }
.words-table tr:hover td { background: var(--panel-2); }
.word-cell { font-weight: 600; }
.word-cell .badges { display: inline-flex; gap: 4px; margin-left: 6px; }
.badge { font-size: 0.75em; padding: 2px 6px; border-radius: 4px; background: var(--bg-2); color: var(--muted); }
.badge.fav { color: var(--warn); }
.badge.hard { color: var(--bad); }
.status-pill { font-size: 0.8em; padding: 2px 8px; border-radius: 999px; }
.status-new { background: #2a3252; color: #93a5d4; }
.status-learning { background: #3a2e1e; color: var(--warn); }
.status-learned { background: #1e3a2c; color: var(--good); }
.row-actions button { padding: 4px 8px; font-size: 0.85em; margin-right: 4px; }
.audio-btn { padding: 2px 6px; font-size: 0.9em; margin-left: 6px; background: var(--bg-2); border: 1px solid var(--border); border-radius: 6px; cursor: pointer; }
.audio-btn:hover { background: var(--panel-2); border-color: var(--accent); }
.audio-btn-header { padding: 4px 10px; font-size: 1.05em; margin: 0; }
.audio-btn-header.playing { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Karaoke transcript highlight */
.tx-word { padding: 1px 2px; border-radius: 3px; transition: background 80ms; }
.tx-word.tx-active { background: #facc15; color: #1c1208; font-weight: 600; }
.tx-word.tx-past { color: var(--muted); }
#tx-content.tx-editing { outline: 2px solid var(--accent); background: #1c2238; cursor: text; }
.tx-shadow-active { background: rgba(99, 102, 241, 0.15); border-radius: 4px; box-shadow: 0 0 0 2px var(--accent); }
.tx-shadow-banner { padding: 8px 12px; background: var(--accent); color: #fff; border-radius: 6px; margin-top: 8px; text-align: center; font-weight: 600; animation: pulse 1.5s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.6; } }
#tx-shadow.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.empty { padding: 60px; text-align: center; color: var(--muted); }

/* Heatmap */
.heatmap {
  display: grid; grid-template-columns: repeat(16, 1fr); gap: 3px;
  max-width: 560px;
}
.heatmap-col { display: grid; grid-template-rows: repeat(7, 14px); gap: 3px; }
.heatmap-cell { background: var(--panel); border-radius: 3px; }
.heatmap-cell.l1 { background: #1e3a2c; }
.heatmap-cell.l2 { background: #2b5a3f; }
.heatmap-cell.l3 { background: #4ade80; }
.heatmap-cell.l4 { background: #86efac; }

.hardest { padding-left: 20px; }
.hardest li { margin: 4px 0; }
.hardest .word { font-weight: 600; }
.hardest .meta { color: var(--muted); font-size: 0.85em; margin-left: 8px; }

/* Settings */
.setting-row { display: flex; gap: 12px; align-items: center; margin: 10px 0; }
.setting-row label { min-width: 180px; color: var(--muted); }
.setting-row input, .setting-row select { flex: 1; max-width: 400px; }

/* Courses */
.course-header { display: flex; align-items: center; gap: 16px; margin-bottom: 8px; }
.course-header h1 { margin: 0; }

#course-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 14px; margin-top: 16px; }
.course-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px; cursor: pointer;
  transition: border-color 0.15s, transform 0.15s;
}
.course-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.course-card h3 { margin: 0 0 4px; }
.course-card .meta { color: var(--muted); font-size: 0.85em; margin-bottom: 12px; }
.course-card .progress-mini { height: 6px; background: var(--bg-2); border-radius: 999px; overflow: hidden; margin: 8px 0; }
.course-card .progress-mini > div { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); }
.course-card .stats-line { display: flex; gap: 16px; color: var(--muted); font-size: 0.85em; }

.milestone-controls { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin: 14px 0; padding: 10px 14px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); }
.ms-mode-pick { display: flex; gap: 8px; align-items: center; }

.unit-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.unit-cell {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px; cursor: pointer;
  text-align: left; transition: border-color 0.15s, background 0.15s;
  position: relative;
}
.unit-cell:hover { border-color: var(--accent); background: var(--panel-2); }
.unit-cell .u-num { font-size: 0.75em; color: var(--muted); }
.unit-cell .u-title { font-weight: 600; font-size: 0.9em; margin: 2px 0; line-height: 1.2; }
.unit-cell .u-topic { font-size: 0.75em; color: var(--accent); }
.unit-cell .u-bar { height: 4px; background: var(--bg-2); border-radius: 999px; margin-top: 6px; overflow: hidden; }
.unit-cell .u-bar > div { height: 100%; background: var(--good); }
.unit-cell.passed { border-color: var(--good); }
.unit-cell.passed::after { content: '✓'; position: absolute; top: 6px; right: 8px; color: var(--good); font-weight: 700; }
.unit-cell .u-actions { position: absolute; top: 4px; right: 4px; }
.unit-cell.passed .u-actions { right: 22px; }
.unit-cell .u-actions button { padding: 2px 5px; font-size: 0.85em; line-height: 1; background: transparent; border: 1px solid var(--border); }
.unit-cell .u-actions button:hover { background: var(--panel-2); border-color: var(--accent); }

.fullwords-table td { vertical-align: top; font-size: 0.9em; padding: 6px 10px; }
.fullwords-table td.fw-word { font-weight: 600; }
.fullwords-table td.fw-meaning input { width: 100%; background: transparent; border: 1px dashed var(--border); padding: 3px 6px; }
.fullwords-table td.fw-meaning input:focus { border-style: solid; border-color: var(--accent); background: var(--bg-2); }
.fullwords-table td.fw-example { color: var(--muted); font-style: italic; font-size: 0.85em; }
.fullwords-table td.fw-actions button { padding: 2px 6px; font-size: 0.85em; }

.block-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; }
.block-cell {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 14px; cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.block-cell:hover:not(.locked) { border-color: var(--accent); background: var(--panel-2); }
.block-cell.locked { opacity: 0.45; cursor: not-allowed; }
.block-cell.passed { border-color: var(--good); }
.block-cell .b-title { font-weight: 600; margin-bottom: 4px; }
.block-cell .b-meta { font-size: 0.85em; color: var(--muted); }
.block-cell .b-score { font-size: 0.85em; margin-top: 4px; }
.block-cell .b-score.good { color: var(--good); }
.block-cell .b-score.bad { color: var(--bad); }

/* Topic picker (collapsible tree) */
.topic-picker { position: relative; min-width: 240px; }
.topic-picker-btn {
  width: 100%; text-align: left;
  background: var(--bg-2); border: 1px solid var(--border);
  padding: 8px 12px; border-radius: 8px;
  display: flex; align-items: center; gap: 8px;
}
.topic-picker-btn:hover { border-color: var(--accent); }
.topic-picker-btn .caret { margin-left: auto; color: var(--muted); }
.topic-picker-btn span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 280px; }
.topic-picker-pop {
  position: absolute; top: calc(100% + 4px); left: 0;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; box-shadow: var(--shadow);
  min-width: 360px; max-height: 60vh; overflow-y: auto;
  padding: 6px 0; z-index: 50;
}
.tp-row {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 10px; cursor: pointer;
  font-size: 0.92em;
}
.tp-row:hover { background: var(--panel-2); }
.tp-row.selected { background: var(--bg-2); color: var(--accent); font-weight: 600; }
.tp-row .tp-toggle {
  display: inline-flex; align-items: center; justify-content: center;
  width: 18px; height: 18px; border: 1px solid var(--border);
  border-radius: 4px; background: var(--bg-2); color: var(--muted);
  font-size: 0.85em; font-weight: 700; user-select: none;
}
.tp-row .tp-toggle:hover { color: var(--text); border-color: var(--accent); }
.tp-row .tp-toggle.placeholder { visibility: hidden; }
.tp-row .tp-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tp-row .tp-count { color: var(--muted); font-size: 0.85em; }
.tp-row.tp-unit { padding-left: 28px; }
.tp-row.tp-course { font-weight: 600; }

/* Auth widget */
.auth-widget { display: flex; align-items: center; gap: 8px; margin-left: 8px; }
.auth-login { background: #4285f4; color: #fff; padding: 6px 12px; border-radius: 6px; text-decoration: none; font-size: 0.85em; font-weight: 600; }
.auth-login:hover { background: #3574e0; }
.auth-user { display: flex; align-items: center; gap: 8px; background: var(--panel); border: 1px solid var(--border); padding: 4px 10px; border-radius: 999px; }
.auth-user img { width: 24px; height: 24px; border-radius: 50%; }
.auth-user span { font-size: 0.85em; max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.auth-user button { background: transparent; border: 0; color: var(--muted); cursor: pointer; padding: 0 4px; font-size: 1em; }
.auth-user button:hover { color: var(--text); }

/* Admin-only elements hidden in user mode */
body.user-mode .admin-only { display: none !important; }
body.admin-mode .admin-badge {
  display: inline-block; background: #f97316; color: #fff;
  padding: 2px 8px; border-radius: 4px; font-size: 0.7em;
  margin-left: 8px; vertical-align: middle; font-weight: 700; letter-spacing: 0.5px;
}

/* Modal */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px;
  width: 92%; max-width: 520px;
  max-height: 90vh; overflow-y: auto;
  box-shadow: var(--shadow);
}
.modal-card h3 { margin: 0 0 16px; }
.form-row { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.form-row label { color: var(--muted); font-size: 0.85em; }
.form-row.checkboxes { flex-direction: row; gap: 18px; align-items: center; }
.form-row.checkboxes label { color: var(--text); font-size: 0.95em; display: flex; gap: 6px; align-items: center; }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; align-items: center; }
.modal-actions .spacer { flex: 1; }

/* Toast */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 10px 18px; border-radius: 8px; z-index: 200;
  box-shadow: var(--shadow);
  animation: slideUp 0.2s;
}
@keyframes slideUp { from { transform: translate(-50%, 20px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }

@media (max-width: 720px) {
  .tabs { overflow-x: auto; }
  .mcq-options { grid-template-columns: 1fr; }
  .match-grid { grid-template-columns: repeat(2, 1fr); }
  .card-word { font-size: 1.8em; }
  .words-table th:nth-child(3), .words-table td:nth-child(3),
  .words-table th:nth-child(6), .words-table td:nth-child(6) { display: none; }
}
