:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #1e3a8a;
  --accent-soft: #e0e7ff;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgb(15 23 42 / 0.05), 0 4px 16px rgb(15 23 42 / 0.05);
  --radius: 10px;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #0f172a;
    --border: #1f2937;
    --border-strong: #334155;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --accent: #93c5fd;
    --accent-soft: #1e293b;
    --danger: #f87171;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #0f172a;
  --border: #1f2937;
  --border-strong: #334155;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --accent: #93c5fd;
  --accent-soft: #1e293b;
  --danger: #f87171;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 Inter, system-ui, -apple-system, sans-serif;
  display: flex;
  flex-direction: column;
}

button, input, select, textarea { font: inherit; color: inherit; }
kbd {
  font-family: var(--mono);
  font-size: 11px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 4px;
  background: var(--surface);
}
code { font-family: var(--mono); font-size: 0.92em; }

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 17px; margin: 0; letter-spacing: -0.01em; }
.brand p { margin: 0; color: var(--muted); font-size: 12px; }
.logo { width: 34px; height: 34px; fill: none; stroke: var(--accent); stroke-width: 2.2; stroke-linecap: round; }
.logo .fill { fill: var(--accent); stroke: none; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.field-inline { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }

select, .btn {
  height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  padding: 0 10px;
  cursor: pointer;
}
.btn { font-weight: 500; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); border-color: var(--border); }
.btn.icon { width: 32px; padding: 0; font-size: 16px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--surface); }
.btn.primary:hover { filter: brightness(1.1); background: var(--accent); }

/* Layout */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(340px, 440px) 1fr;
  gap: 16px;
  padding: 16px;
}
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}

/* Tabs */
.tabs { display: flex; gap: 2px; padding: 6px 6px 0; border-bottom: 1px solid var(--border); }
.tab {
  border: 0;
  background: none;
  padding: 8px 12px;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  cursor: pointer;
  font-weight: 500;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-body { padding: 16px; overflow: auto; flex: 1; min-height: 0; }

.problem-label { display: block; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--danger); margin-bottom: 6px; }
.problem-input {
  width: 100%;
  font-size: 16px;
  font-weight: 600;
  padding: 10px 12px;
  border: 2px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
}
.problem-input:focus { outline: none; border-color: var(--danger); }

.section-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 12px; margin: 20px 0 10px; }
.section-head h2 { font-size: 13px; margin: 0; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.hint { font-size: 11.5px; color: var(--muted); }

/* Category cards */
.category {
  --cat: #1e40af;
  border: 1px solid var(--border);
  border-left: 4px solid var(--cat);
  border-radius: 8px;
  margin-bottom: 10px;
  background: var(--surface);
}
.category-head { display: flex; align-items: center; gap: 6px; padding: 6px 6px 6px 10px; background: color-mix(in srgb, var(--cat) 8%, transparent); border-radius: 0 7px 0 0; }
.category-head input {
  flex: 1;
  min-width: 0;
  border: 1px solid transparent;
  background: transparent;
  font-weight: 700;
  color: var(--cat);
  padding: 4px 6px;
  border-radius: 6px;
}
:root[data-theme="dark"] .category-head input { color: color-mix(in srgb, var(--cat) 55%, white); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .category-head input { color: color-mix(in srgb, var(--cat) 55%, white); }
}
.category-head input:focus { outline: none; border-color: var(--cat); background: var(--surface); }
.count { font-size: 11px; color: var(--muted); padding: 0 4px; }

.causes { list-style: none; margin: 0; padding: 4px 6px 6px 10px; }
.causes .causes { padding: 0 0 0 18px; border-left: 1px dashed var(--border-strong); margin-left: 8px; }
.cause-row { display: flex; align-items: center; gap: 4px; padding: 1px 0; }
.cause-row::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--cat); flex: none; opacity: 0.7; }
.causes .causes .cause-row::before { background: transparent; border: 1.5px solid var(--cat); width: 5px; height: 5px; }
.cause-row input {
  flex: 1;
  min-width: 0;
  border: 1px solid transparent;
  background: transparent;
  padding: 3px 6px;
  border-radius: 6px;
}
.cause-row input:hover { border-color: var(--border); }
.cause-row input:focus { outline: none; border-color: var(--border-strong); background: var(--surface-2); }
.row-actions { display: flex; gap: 2px; opacity: 0; transition: opacity 0.1s; }
.cause-row:hover .row-actions, .cause-row:focus-within .row-actions,
.category-head:hover .row-actions, .category-head:focus-within .row-actions { opacity: 1; }
@media (hover: none) { .row-actions { opacity: 1; } }
.mini {
  height: 24px;
  min-width: 24px;
  padding: 0 6px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
}
.mini:hover { background: var(--surface-2); border-color: var(--border); color: var(--text); }
.mini.why { color: var(--cat); font-weight: 600; }
.mini.del:hover { color: var(--danger); }
.add-cause { margin: 0 0 6px 16px; font-size: 12px; color: var(--muted); }
.add-category { width: 100%; border-style: dashed; color: var(--muted); }

/* Code tab */
#code-input {
  width: 100%;
  min-height: 420px;
  height: calc(100% - 40px);
  resize: vertical;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.6;
  padding: 12px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface-2);
  tab-size: 4;
}
#code-input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12.5px; margin-top: 0; }
.error { color: var(--danger); font-size: 12.5px; white-space: pre-wrap; font-family: var(--mono); }
.error.floating {
  position: absolute;
  left: 16px; right: 16px; bottom: 16px;
  margin: 0;
  background: var(--surface);
  border: 1px solid var(--danger);
  border-radius: 8px;
  padding: 8px 12px;
}

/* Guide */
.guide h2 { font-size: 16px; margin-top: 0; }
.guide h3 { font-size: 14px; margin-top: 20px; }
.guide ol { padding-left: 20px; }
.guide li { margin-bottom: 8px; }
.pill { background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 1px 8px; font-weight: 600; font-size: 12px; }
.ms { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; margin: 0 0 16px; }
.ms dt { font-weight: 600; }
.ms dd { margin: 0; color: var(--muted); }

/* Preview */
.preview-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.preview-toolbar .group { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.preview {
  flex: 1;
  min-height: 360px;
  overflow: auto;
  background-image: radial-gradient(var(--border) 1px, transparent 1px);
  background-size: 18px 18px;
  border-radius: 0 0 var(--radius) var(--radius);
}
.preview-inner { padding: 24px; min-height: 100%; display: flex; align-items: center; justify-content: center; }
.preview-inner svg { display: block; height: auto; }
.preview-inner.fit svg { max-width: 100%; }
.preview.empty .preview-inner::after { content: "Add a problem to see your fishbone."; color: var(--muted); }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: var(--text);
  color: var(--surface);
  padding: 8px 14px;
  border-radius: 8px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 900px) {
  html, body { height: auto; }
  .layout { flex: none; grid-template-columns: 1fr; padding: 16px; }
  .tab-body, .preview { flex: none; }
  .tab-body { overflow: visible; }
  .preview { min-height: 420px; }
  .topbar { padding: 12px 16px; }
}

@media print {
  .topbar, .editor-panel, .preview-toolbar { display: none; }
  .layout { display: block; padding: 0; }
  .panel { border: 0; box-shadow: none; }
  .preview { background: none; overflow: visible; }
}
