:root {
  --bg: #ffffff;
  --card-bg: #fbfcfb;
  --fg: #1d2b24;
  --muted: #62726a;
  --line: #dfe5e1;
  --line-light: #edf2ee;
  --accent: #1f6f4f;
  --accent-hover: #195c41;
  --accent-light: rgba(31, 111, 79, 0.08);
  --accent-fg: #ffffff;
  --warn: #c25e0a;
  --bad: #b3261e;
  --ok: #1f6f4f;
  --code: #f3f6f4;
  --focus-ring: 0 0 0 2px rgba(31, 111, 79, 0.2);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c211e;
    --card-bg: #222925;
    --fg: #e6ece8;
    --muted: #9aa9a1;
    --line: #334039;
    --line-light: #28322d;
    --accent: #4fb58a;
    --accent-hover: #5ec498;
    --accent-light: rgba(79, 181, 138, 0.12);
    --accent-fg: #0a2016;
    --warn: #e2a05e;
    --bad: #f28b82;
    --ok: #6fd0a6;
    --code: #252d28;
    --focus-ring: 0 0 0 2px rgba(79, 181, 138, 0.3);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 12.5px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }

/* --- Sticky Modern Navigation --- */
nav {
  display: flex;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  gap: 2px;
  border-bottom: 1px solid var(--line);
  padding: 0 6px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}
nav::-webkit-scrollbar { display: none; }

nav button {
  border: 0;
  background: none;
  color: var(--muted);
  padding: 6px 8px;
  font-size: 11.5px;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s, background-color 0.15s;
  border-radius: 4px 4px 0 0;
}
nav button:hover {
  color: var(--fg);
  background: var(--accent-light);
}
nav button[aria-selected="true"] {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

/* --- Sections --- */
section {
  padding: 10px 12px 24px;
  display: none;
}
section.active {
  display: block;
}

/* --- Fieldset / Cards --- */
fieldset {
  border: 1px solid var(--line);
  background: var(--card-bg);
  border-radius: 8px;
  margin: 0 0 10px;
  padding: 8px 10px 10px;
}
legend {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--fg);
  padding: 0 6px;
  letter-spacing: -0.1px;
}

/* --- Notices & Messages --- */
.notice {
  border-left: 3px solid var(--warn);
  padding: 6px 9px;
  margin: 0 0 10px;
  border-radius: 0 6px 6px 0;
  background: var(--code);
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--muted);
}
.notice b { color: var(--fg); }

/* --- Forms & Inputs --- */
label {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  margin: 5px 0;
  font-size: 12px;
  line-height: 1.4;
  cursor: pointer;
  user-select: none;
}
label input[type=checkbox],
label input[type=radio] {
  margin: 2px 0 0;
  flex-shrink: 0;
  accent-color: var(--accent);
  cursor: pointer;
}
label.inline {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-right: 10px;
}

input[type=text],
input[type=number],
select,
textarea {
  width: 100%;
  padding: 5px 7px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--bg);
  color: var(--fg);
  font: inherit;
  font-size: 12px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input[type=text]:focus,
input[type=number]:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--focus-ring);
}
input[type=number] { width: 75px; }

/* --- Buttons --- */
button.primary,
button.secondary {
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  padding: 5px 11px;
  border-radius: 5px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1.35;
  transition: background-color 0.15s, border-color 0.15s, color 0.15s, box-shadow 0.15s, transform 0.05s;
}
button.primary {
  background: var(--accent);
  color: var(--accent-fg);
  border: 1px solid var(--accent);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}
button.primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
button.primary:active:not(:disabled) {
  transform: translateY(1px);
}
button.secondary {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--line);
}
button.secondary:hover:not(:disabled) {
  background: var(--code);
  border-color: var(--accent);
  color: var(--accent);
}
button.secondary:active:not(:disabled) {
  transform: translateY(1px);
}
button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  pointer-events: none;
}

/* --- Row & Layout Utils --- */
.row {
  display: flex;
  gap: 6px 8px;
  flex-wrap: wrap;
  align-items: center;
  margin: 6px 0;
}
.muted { color: var(--muted); }
.warn  { color: var(--warn); }
.bad   { color: var(--bad); }
.ok    { color: var(--ok); }

progress {
  width: 100%;
  height: 6px;
  border-radius: 3px;
  overflow: hidden;
  accent-color: var(--accent);
}

/* --- Tables & Lists --- */
.scroll {
  max-height: 240px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
}
table {
  border-collapse: collapse;
  width: 100%;
  font-size: 11.5px;
}
th {
  position: sticky;
  top: 0;
  background: var(--code);
  z-index: 1;
  font-weight: 600;
  color: var(--fg);
  padding: 5px 6px;
  text-align: left;
  border-bottom: 1px solid var(--line);
}
td {
  text-align: left;
  border-bottom: 1px solid var(--line-light);
  padding: 4px 6px;
  vertical-align: top;
}
tr:hover td {
  background: var(--accent-light);
}

code, .tex {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  background: var(--code);
  border-radius: 3px;
  padding: 1px 4px;
  word-break: break-all;
}

.badge {
  font-size: 10.5px;
  font-weight: 500;
  padding: 1px 5px;
  border-radius: 8px;
  border: 1px solid var(--line);
  white-space: nowrap;
  display: inline-block;
}
.badge.ok { color: var(--ok); border-color: var(--ok); }
.badge.bad { color: var(--bad); border-color: var(--bad); }
.badge.native { color: var(--muted); }

.log {
  max-height: 200px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 8px;
  background: var(--code);
  white-space: pre-wrap;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 11px;
  line-height: 1.4;
}
.diff del { color: var(--bad); text-decoration: none; }
.diff ins { color: var(--ok); text-decoration: none; }

/* --- Formula Editor --- */
.formula-preview {
  min-height: 52px;
  background: var(--card-bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-x: auto;
  font-size: 1.15em;
  margin-bottom: 8px;
}
.formula-preview:empty::before {
  content: "Xem trước công thức sẽ hiển thị tại đây";
  color: var(--muted);
  font-size: 11.5px;
  font-style: italic;
}

#editor-latex {
  width: 100%;
  min-height: 65px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  padding: 6px 8px;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--fg);
  resize: vertical;
}

.sym-tabs {
  display: flex;
  gap: 3px;
  margin-bottom: 6px;
  overflow-x: auto;
}
.sym-tab-btn {
  border: 1px solid var(--line);
  background: var(--card-bg);
  color: var(--muted);
  border-radius: 4px;
  padding: 2px 7px;
  font-size: 11px;
  cursor: pointer;
  white-space: nowrap;
}
.sym-tab-btn.active {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
  font-weight: 600;
}

.sym-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px;
  background: var(--card-bg);
  border: 1px solid var(--line);
  border-radius: 4px;
  max-height: 120px;
  overflow-y: auto;
}
.sym-btn {
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--fg);
  border-radius: 4px;
  padding: 3px 6px;
  font-size: 12px;
  cursor: pointer;
  line-height: 1.2;
  min-width: 28px;
  text-align: center;
  transition: all 0.1s;
  user-select: none;
}
.sym-btn:hover {
  background: var(--accent-light);
  border-color: var(--accent);
  color: var(--accent);
}

/* LaTeX autocomplete popup */
.ac-popup { position: fixed; z-index: 10000; min-width: 240px; max-width: min(360px, 96vw); max-height: 260px; overflow: auto; background: var(--bg, #fff); color: var(--fg, #111); border: 1px solid var(--line, #ccc); border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.18); font-size: 12px; }
.ac-item { display: flex; align-items: center; gap: 8px; padding: 5px 9px; cursor: pointer; }
.ac-item.sel, .ac-item:hover { background: color-mix(in srgb, var(--accent, #1f6f4f) 16%, transparent); }
.ac-glyph { width: 20px; text-align: center; font-size: 15px; font-family: 'Cambria Math', 'STIX Two Math', 'Times New Roman', serif; }
.ac-item code { font-size: 12px; background: none; padding: 0; white-space: nowrap; }
.ac-desc { color: var(--muted, #666); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ac-hint { padding: 4px 9px; color: var(--muted, #666); font-size: 11px; border-top: 1px solid var(--line, #ddd); }

/* ---------- Equation editor: professional toolbar ---------- */
.editor-toolbar { margin-top: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); overflow: hidden; }
.tb-search { display: flex; align-items: center; gap: 6px; padding: 6px 8px; border-bottom: 1px solid var(--line); color: var(--muted); }
.tb-search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--fg); font: inherit; padding: 2px 0; }
.tb-recent { display: flex; align-items: center; gap: 6px; padding: 4px 8px; border-bottom: 1px solid var(--line); background: color-mix(in srgb, var(--accent) 5%, transparent); }
.tb-recent-label { font-size: 11px; color: var(--muted); white-space: nowrap; }
.tb-tabs { display: flex; gap: 2px; padding: 4px 4px 0; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.tb-tabs::-webkit-scrollbar { display: none; }
.tb-tab { flex: 0 0 auto; display: inline-flex; align-items: center; gap: 5px; border: 0; border-bottom: 2px solid transparent; background: none; color: var(--muted); padding: 5px 8px; cursor: pointer; font: inherit; font-size: 12px; border-radius: 6px 6px 0 0; }
.tb-tab:hover { background: color-mix(in srgb, var(--accent) 8%, transparent); color: var(--fg); }
.tb-tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.tb-tab-icon { display: inline-flex; align-items: center; height: 16px; }
.tb-tab-icon .tb-svg { height: 19px; width: auto; }
.tb-tab-icon { height: 20px; }
.tb-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); gap: 4px; padding: 8px; }
.tb-recent .tb-grid { flex: 1; padding: 0; grid-template-columns: repeat(auto-fill, minmax(34px, 1fr)); }
.tb-btn { height: 34px; display: inline-flex; align-items: center; justify-content: center; padding: 2px 4px; border: 1px solid var(--line); border-radius: 7px; background: var(--bg); color: var(--fg); cursor: pointer; transition: background .12s, border-color .12s, transform .05s; }
.tb-btn:hover { background: color-mix(in srgb, var(--accent) 10%, transparent); border-color: var(--accent); }
.tb-btn:active { transform: scale(.96); }
.tb-btn .tb-svg { height: 24px; max-width: 100%; width: auto; }
.tb-text { font: 12px ui-monospace, Menlo, monospace; }
.tb-empty, .tb-tip { padding: 8px; color: var(--muted); font-size: 11.5px; grid-column: 1 / -1; }
.tb-tip { border-top: 1px solid var(--line); }
.tb-tip code { font-size: 11px; }
.tb-action { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
button.primary svg { vertical-align: -2px; }
.seg-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.seg-title { color: var(--muted); font-size: 12px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg-opt { margin: 0; cursor: pointer; }
.seg-opt input { position: absolute; opacity: 0; pointer-events: none; }
.seg-opt span { display: block; padding: 5px 10px; font-size: 12px; color: var(--muted); border-right: 1px solid var(--line); }
.seg-opt:last-child span { border-right: 0; }
.seg-opt input:checked + span { background: var(--accent); color: var(--accent-fg); font-weight: 600; }
.seg-opt input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
.switch { display: flex; align-items: center; gap: 8px; margin: 6px 0; cursor: pointer; }
.switch input { position: absolute; opacity: 0; }
.switch-track { position: relative; flex: 0 0 auto; width: 30px; height: 17px; border-radius: 9px; background: var(--line); transition: background .15s; }
.switch-track::after { content: ''; position: absolute; top: 2px; left: 2px; width: 13px; height: 13px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.3); }
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(13px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Narrow task pane (the pane can be as thin as ~300 px) ---------- */
html, body { max-width: 100%; overflow-x: hidden; }
nav { overflow-x: auto; scrollbar-width: none; flex-wrap: nowrap; }
nav::-webkit-scrollbar { display: none; }
nav button { flex: 0 0 auto; padding: 8px 9px; white-space: nowrap; }
img, svg, textarea, input, select, .scroll, table { max-width: 100%; }
fieldset { min-width: 0; }
.scroll { overflow-x: auto; }
@media (max-width: 420px) {
  section { padding: 10px 8px 20px; }
  fieldset { padding: 6px 8px 8px; }
  nav { padding: 0 4px; }
  nav button { padding: 8px 7px; font-size: 12px; }
  .tb-tab { padding: 5px 7px; }
  .tb-tab:not(.active) .tb-tab-label { display: none; }
  .tb-grid { grid-template-columns: repeat(auto-fill, minmax(34px, 1fr)); padding: 6px; gap: 3px; }
  .tb-btn { height: 32px; }
  .tb-btn .tb-svg { height: 22px; }
  .row { gap: 6px; }
  .row > button { flex: 1 1 auto; }
  th, td { padding: 3px 3px; }
  table { font-size: 11px; }
}
@media (max-width: 340px) {
  .tb-tab .tb-tab-label { display: none !important; }
  .seg-opt span { padding: 5px 7px; }
}

.kbd { margin-left: 8px; padding: 0 6px; font: 11px ui-monospace, Menlo, monospace; border: 1px solid color-mix(in srgb, currentColor 45%, transparent); border-radius: 4px; opacity: .85; }

.chips-title { margin: 8px 0 4px; font-size: 12px; color: var(--warn); font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip { padding: 3px 9px; border-radius: 999px; border: 1px solid var(--warn); background: color-mix(in srgb, var(--warn) 10%, transparent); color: var(--fg); font: inherit; font-size: 11.5px; cursor: pointer; }
.chip:hover { background: color-mix(in srgb, var(--warn) 22%, transparent); }

/* ---------- Balanced layout components ---------- */
.field { display: flex; flex-direction: column; gap: 4px; margin: 8px 0; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 600; }
.field input[type=text], .field input[type=number], .field select { width: 100%; }
.grid2, .grid3 { display: grid; gap: 8px; }
.grid2 { grid-template-columns: 1fr 1fr; }
.grid3 { grid-template-columns: repeat(3, 1fr); }
.grid3 .field input { width: 100%; }
.btns { display: grid; grid-template-columns: 1fr; gap: 8px; margin: 10px 0; }
.btns.two { grid-template-columns: 1fr 1fr; }
.btns.four { grid-template-columns: repeat(2, 1fr); }
.btns > button { width: 100%; min-height: 34px; }
.subhead { margin: 14px 0 2px; padding-top: 10px; border-top: 1px solid var(--line); font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.orig-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 6px 0; }
.orig-row .switch { margin: 0; }
.orig-row input[type=text] { width: 72px; text-align: center; font-variant-numeric: tabular-nums; }
.swatch { display: flex; align-items: center; gap: 4px; margin: 4px 0 8px; flex-wrap: wrap; }
.qr-panel { text-align: center; margin: 8px 0; }
.hint { color: var(--muted); font-size: 11.5px; margin: 2px 0 8px; }
fieldset > label:not(.field):not(.switch):not(.seg-opt) { display: flex; align-items: flex-start; gap: 7px; margin: 5px 0; line-height: 1.35; }
fieldset > label:not(.field) input[type=checkbox] { margin-top: 2px; flex: 0 0 auto; }
@media (max-width: 340px) { .grid3 { grid-template-columns: 1fr 1fr; } .btns.two { grid-template-columns: 1fr; } }

.scan-summary { margin: 2px 0 8px; font-size: 12px; }
fieldset > .seg-row { margin: 6px 0; }
.seg { max-width: 100%; }
.seg-opt span { white-space: nowrap; }

.tech { margin: 4px 0 12px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.tech summary { cursor: pointer; font-weight: 600; color: var(--muted); }
.tech[open] summary { margin-bottom: 8px; }

.fig-tabs { margin-bottom: 8px; border: 1px solid var(--line); border-radius: 8px 8px 0 0; }
.fig-preview { margin: 10px 0; padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: #fff; overflow: auto; text-align: center; min-height: 80px; }
.fig-preview svg { max-width: 100%; height: auto; }
textarea { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; }

.calc-card { margin-top: 10px; }
.calc-input { font-size: 11.5px; margin-bottom: 2px; overflow-wrap: anywhere; }
.calc-result { overflow-x: auto; padding: 2px 0; }
.calc-result .katex-display { margin: 4px 0; }
.calc-extra { font-size: 12px; color: var(--muted); border-top: 1px dashed var(--line); padding-top: 4px; overflow-x: auto; }
.calc-out { margin-top: 4px; font-size: 12px; }
