:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --text: #1f2328;
  --muted: #6b7280;
  --line: #e4e2dc;
  --primary: #2f6fed;
  --primary-ink: #ffffff;
  --danger: #e5484d;
  --ok: #2e9e5b;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.06);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); display: flex; flex-direction: column; overflow: hidden; }
[hidden] { display: none !important; }

/* Buttons */
.btn {
  font: inherit; font-size: 14px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 8px; white-space: nowrap;
}
.btn:hover { border-color: #c9c6be; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn.small { font-size: 13px; padding: 5px 10px; }
.icon { border: 0; background: transparent; cursor: pointer; font-size: 18px; line-height: 1; color: var(--muted); padding: 2px 6px; border-radius: 6px; }
.icon:hover { background: rgba(0,0,0,.06); color: var(--text); }

/* Top & bottom bars */
.topbar, .bottombar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.bottombar { border-bottom: 0; border-top: 1px solid var(--line); }
.brand { display: flex; flex-direction: column; min-width: 0; }
.brand strong { font-size: 16px; }
.brand span { font-size: 12px; color: var(--muted); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.status { font-size: 13px; color: var(--muted); }

/* Board */
.viewport { flex: 1; overflow: auto; position: relative; }
.board {
  position: relative; width: 2400px; height: 1600px;
  background-image: radial-gradient(#d6d3cb 1px, transparent 1px);
  background-size: 22px 22px;
}
.empty-hint {
  position: absolute; left: 40px; top: 40px; max-width: 560px;
  background: var(--surface); border: 1px dashed #c9c6be; border-radius: var(--radius);
  padding: 20px 24px; color: var(--muted); font-size: 14px; line-height: 1.55;
}
.empty-hint h2 { margin: 0 0 6px; color: var(--text); font-size: 18px; }
.empty-hint ul { padding-left: 18px; margin: 10px 0 0; }
kbd { font: 12px ui-monospace, monospace; background: #f0eee9; border: 1px solid var(--line); border-radius: 4px; padding: 1px 5px; }

/* Sticky note */
.note {
  position: absolute; width: 220px;
  border-radius: 6px; box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  transition: box-shadow .15s;
}
.note.dragging { box-shadow: 0 10px 28px rgba(0,0,0,.18); }
.note.active { outline: 2px solid var(--primary); outline-offset: 2px; }
.note-head { display: flex; align-items: center; gap: 6px; padding: 4px 4px 0 8px; cursor: grab; touch-action: none; user-select: none; }
.note.dragging .note-head { cursor: grabbing; }
.grip { color: rgba(0,0,0,.35); font-size: 12px; letter-spacing: -2px; }
.badge { flex: 1; font-size: 11px; font-weight: 600; color: rgba(0,0,0,.55); }
.badge.done { color: var(--ok); }
.badge.stale { color: #b45309; }
.note-text {
  font: inherit; font-size: 14px; line-height: 1.45; color: var(--text);
  border: 0; background: transparent; resize: none; outline: none;
  min-height: 96px; padding: 6px 10px; field-sizing: content; max-height: 260px;
}
.insight-btn { margin: 0 10px 10px; align-self: flex-start; background: rgba(255,255,255,.7); border-color: rgba(0,0,0,.12); }

/* Insight side panel */
.panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(420px, 100vw);
  background: var(--surface); border-left: 1px solid var(--line);
  box-shadow: -8px 0 24px rgba(0,0,0,.08); z-index: 20;
  display: flex; flex-direction: column;
}
.panel-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.panel-head h3 { margin: 0; font-size: 16px; }
.panel-body { flex: 1; overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.panel-foot { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.quote { background: #fffbe0; border-left: 3px solid #e3c840; padding: 10px 12px; border-radius: 6px; white-space: pre-wrap; font-size: 14px; }
.qa { font-size: 14px; line-height: 1.5; }
.qa .q { font-weight: 600; }
.qa .a { color: var(--muted); white-space: pre-wrap; }
.ask label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; line-height: 1.45; }
.ask textarea {
  width: 100%; font: inherit; font-size: 14px; padding: 8px 10px; min-height: 60px;
  border: 1px solid var(--line); border-radius: 8px; resize: vertical;
}
.ask textarea:focus { outline: 2px solid var(--primary); border-color: transparent; }
.ask + .ask { margin-top: 12px; }
.hint { font-size: 12px; color: var(--muted); }
.warn-box { background: #fff4e5; color: #92400e; padding: 8px 12px; border-radius: 8px; font-size: 13px; }
.thinking { color: var(--muted); font-size: 14px; display: flex; align-items: center; gap: 8px; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.summary { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.summary h4 { margin: 0; padding: 10px 12px; background: #eef8f1; color: var(--ok); font-size: 14px; }
.summary dl { margin: 0; display: grid; grid-template-columns: 120px 1fr; font-size: 14px; }
.summary dt, .summary dd { margin: 0; padding: 7px 12px; border-top: 1px solid var(--line); }
.summary dt { color: var(--muted); }

/* Result modal */
.modal { position: fixed; inset: 0; background: rgba(20,20,20,.45); z-index: 30; display: flex; padding: 24px; }
.modal-card { background: var(--surface); border-radius: 14px; width: 100%; max-width: 1100px; margin: auto; max-height: 100%; display: flex; flex-direction: column; overflow: hidden; }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; padding: 14px 20px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 18px; }
.result-body { overflow: auto; padding: 20px; }
.result-body h1 { font-size: 22px; margin: 0 0 4px; }
.result-body .meta { color: var(--muted); font-size: 13px; margin-bottom: 16px; }
.result-body h3 { font-size: 16px; margin: 24px 0 10px; }
.diagram { border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; overflow: auto; text-align: center; }
.diagram svg { max-width: 100%; height: auto; }
.legend { font-size: 12px; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 12px; height: 12px; background: #ffe4e1; border: 2px solid var(--danger); border-radius: 3px; vertical-align: -2px; margin-right: 4px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; vertical-align: top; padding: 8px 10px; border-bottom: 1px solid var(--line); }
th { background: #f6f5f1; font-weight: 600; }
td.bn { color: #b42318; }
td.unknown { color: var(--muted); font-style: italic; }
pre.code { background: #f6f5f1; padding: 12px; border-radius: 8px; overflow: auto; font-size: 12px; text-align: left; }

/* Toast */
.toast { position: fixed; left: 50%; bottom: 72px; transform: translateX(-50%); background: #1f2328; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px; z-index: 40; max-width: calc(100vw - 32px); }

@media (max-width: 640px) {
  .brand span { display: none; }
  .modal { padding: 0; }
  .modal-card { border-radius: 0; height: 100%; }
  .summary dl { grid-template-columns: 100px 1fr; }
}

@media print {
  body { overflow: visible; display: block; background: #fff; }
  body > *:not(#resultModal) { display: none !important; }
  .modal { position: static; background: none; padding: 0; display: block; }
  .modal-card { max-height: none; box-shadow: none; border-radius: 0; }
  .no-print { display: none !important; }
  .result-body { overflow: visible; padding: 0; }
  .diagram { break-inside: avoid; }
  tr { break-inside: avoid; }
}
