:root {
  --bg: #f6f7f9; --card: #fff; --ink: #1f2933; --muted: #6b7785; --line: #dde2e8;
  --accent: #2563eb; --accent-ink: #fff; --ok: #15803d; --warn: #b45309; --err: #b91c1c;
  --easy: #dcfce7; --medium: #fef3c7; --hard: #fee2e2; --sel: #eff6ff;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Thonburi", "Segoe UI", Tahoma, sans-serif;
}
button, input, select { font: inherit; }
.loading { padding: 40px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.topbar { display: flex; justify-content: space-between; align-items: center; gap: 16px;
  padding: 10px 20px; background: var(--card); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.brand { font-weight: 700; font-size: 17px; }
.tab { border: 0; background: none; padding: 8px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); }
.tab:hover { background: var(--bg); }
.tab-active { background: var(--sel); color: var(--accent); font-weight: 600; }

main { padding: 16px 20px 60px; max-width: 1400px; margin: 0 auto; }
.banner { margin: 10px 20px 0; padding: 10px 14px; border-radius: 8px; background: #e0f2fe; }
.banner-warn { background: #fef3c7; }
.banner-error { background: #fee2e2; margin: 20px; }

.btn { border: 1px solid var(--line); background: var(--card); padding: 7px 14px; border-radius: 8px; cursor: pointer; }
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-small { padding: 3px 10px; font-size: 13px; }
.btn-link { border: 0; background: none; color: var(--muted); text-decoration: underline; padding: 6px 0; }
.btn-danger { color: var(--err); }
.wide { width: 100%; margin-top: 14px; }

.filters { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; margin-bottom: 14px; }
.frow { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px; padding: 5px 0; }
.flabel { font-weight: 600; }
.chk { display: inline-flex; align-items: center; gap: 2px; cursor: pointer; }
.chk.disabled { opacity: .5; }
.topics { width: 100%; }
.topics summary { cursor: pointer; font-weight: 600; }
.topic-actions { margin: 8px 0; display: flex; gap: 8px; }
/* topic picker: equal-size cells, numbered top-to-bottom then next column */
.topic-grid { columns: 4 280px; column-gap: 12px; padding-top: 4px; }
.topic-cell { display: flex; align-items: flex-start; gap: 8px; box-sizing: border-box;
  height: calc(3em + 16px); padding: 8px 10px; margin-bottom: 8px; break-inside: avoid;
  border: 1px solid var(--line); border-radius: 8px; background: #fafbfc; cursor: pointer; }
.topic-cell:hover { border-color: var(--accent); }
.topic-cell:has(input:checked) { background: var(--sel); border-color: var(--accent); }
.topic-cell input { flex: none; margin: 4px 0 0; }
.topic-cell .tno { flex: none; width: 1.8em; text-align: right; font-weight: 600; color: var(--muted); }
.topic-cell .tname { flex: 1; min-width: 0; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.search { flex: 1; min-width: 220px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; }
.random-box { display: inline-flex; gap: 10px; align-items: center; }
.cnt input { width: 64px; padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px; }

.split { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
.list { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 6px 0; }
.list-head { display: flex; align-items: center; gap: 12px; padding: 6px 14px 10px; border-bottom: 1px solid var(--line); }
.row { border-bottom: 1px solid #eef1f4; }
.row-checked { background: var(--sel); }
.row-head { display: flex; align-items: baseline; gap: 10px; padding: 8px 14px; cursor: pointer; }
.row-head input { transform: translateY(2px); }
.id { font-family: ui-monospace, Menlo, monospace; font-size: 12px; color: var(--muted); white-space: nowrap; }
.lv { font-size: 12px; padding: 1px 8px; border-radius: 10px; white-space: nowrap; }
.lv-Easy { background: var(--easy); } .lv-Medium { background: var(--medium); } .lv-Hard { background: var(--hard); }
.q { flex: 1; }
.used { font-size: 12px; color: var(--warn); white-space: nowrap; }
.more { margin: 12px 14px; }
.detail { padding: 4px 14px 12px 52px; font-size: 14px; }
.opts { margin: 4px 0 8px; padding-left: 22px; }
.opts .correct { font-weight: 700; color: var(--ok); }
.expl { white-space: pre-line; margin-top: 6px; color: #334155; }
.src a { color: var(--accent); }

.panel { position: sticky; top: 70px; background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px;
  display: flex; flex-direction: column; max-height: calc(100vh - 86px); }
.panel-head { flex: none; padding-bottom: 6px; }
.panel-topics { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding-right: 4px; }
.panel-foot { flex: none; border-top: 1px solid var(--line); margin-top: 8px; }
.panel-foot .wide { margin-top: 10px; }
.panel h3 { margin: 0 0 6px; }
.big { font-size: 22px; font-weight: 700; }
.ptopic { margin-top: 10px; padding-top: 8px; border-top: 1px dashed var(--line); }

/* review page */
.review-top { display: flex; flex-wrap: wrap; gap: 10px 18px; align-items: center; background: var(--card);
  border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin-bottom: 14px; }
.review-top input[type=text] { min-width: 320px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; }
.ritem { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; margin-bottom: 8px; }
.ritem-head { display: flex; gap: 10px; align-items: baseline; }
.ritem-head .q { font-weight: 600; }
.ritem-tools { display: flex; gap: 4px; }
.ritem .detail { padding-left: 28px; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin: 6px 0 14px; }

/* history */
.hcard { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin-bottom: 12px; }
.hcard h3 { margin: 0 0 8px; display: flex; gap: 10px; align-items: center; }
table.records { width: 100%; border-collapse: collapse; }
table.records td, table.records th { text-align: left; padding: 6px 8px; border-top: 1px solid #eef1f4; }

dialog { border: 1px solid var(--line); border-radius: 12px; padding: 18px; min-width: 360px; }
dialog label { display: block; margin: 8px 0; }
dialog input, dialog select { width: 100%; padding: 6px 8px; margin-top: 4px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); padding: 10px 18px; border-radius: 10px;
  color: #fff; background: var(--ok); box-shadow: 0 6px 20px rgba(0,0,0,.15); z-index: 50; max-width: 80vw; }
.toast-warn { background: var(--warn); } .toast-error { background: var(--err); }
