/* ---------- theme ---------- */
:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #1c2733;
  --text-dim: #5b6b7b;
  --border: #d4dbe3;
  --accent: #2f6fed;
  --accent-soft: rgba(47, 111, 237, 0.14);
  --danger: #d64545;
  --warn: #d64545;
  --ok: #2e9e5b;
  --grid-dot: #d0d7e0;
  --node-storage: #e2f3e8;
  --node-storage-stroke: #3f9e6a;
  --node-external: #fdeed7;
  --node-external-stroke: #d99a2b;
  --node-process: #ddeafd;
  --node-process-stroke: #3f76d6;
  --flow-stroke: #6b7a8c;
  --badge-bg: #ffffff;
  --shadow: 0 2px 10px rgba(16, 30, 50, 0.12);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181f;
    --surface: #1d232c;
    --surface-2: #242b36;
    --text: #e5eaf0;
    --text-dim: #93a1b1;
    --border: #333d4a;
    --accent: #5b8ff5;
    --accent-soft: rgba(91, 143, 245, 0.2);
    --danger: #e06c6c;
    --warn: #e06c6c;
    --ok: #4cba79;
    --grid-dot: #2a323e;
    --node-storage: #1f3a2c;
    --node-storage-stroke: #4cba79;
    --node-external: #3d2f17;
    --node-external-stroke: #e0a83f;
    --node-process: #1e3252;
    --node-process-stroke: #6b9bf0;
    --flow-stroke: #8b9aab;
    --badge-bg: #242b36;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  overscroll-behavior: none;
}

/* ---------- app layout ---------- */
#app {
  height: 100dvh;
  display: grid;
  grid-template-areas:
    "toolbar toolbar toolbar"
    "palette canvas  panel"
    "status  status  status";
  grid-template-rows: auto 1fr auto;
  grid-template-columns: auto 1fr auto;
}

/* ---------- toolbar ---------- */
#toolbar {
  grid-area: toolbar;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
#logo { font-size: 20px; }
#diagram-title {
  flex: 1 1 120px;
  min-width: 60px;
  max-width: 340px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 6px 8px;
}
#diagram-title:hover { border-color: var(--border); }
#diagram-title:focus { border-color: var(--accent); outline: none; background: var(--surface-2); }
.toolbar-group { display: flex; gap: 4px; }
#toolbar button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  min-width: 34px;
  min-height: 34px;
  justify-content: center;
  font-size: 13px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  cursor: pointer;
}
#toolbar button:hover:not(:disabled) { background: var(--accent-soft); border-color: var(--accent); }
#toolbar button:disabled { opacity: 0.4; cursor: default; }
#zoom-group button { font-weight: 600; }

/* ---------- palette ---------- */
#palette {
  grid-area: palette;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 8px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow: auto;
}
.palette-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  width: 74px;
  padding: 8px 4px 6px;
  font-size: 11.5px;
  color: var(--text-dim);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
}
.palette-btn:hover { background: var(--surface-2); border-color: var(--border); }
.palette-btn.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.palette-btn svg { width: 46px; height: 30px; }
.palette-sep { height: 1px; margin: 4px 6px; background: var(--border); }
.pal-external  { fill: var(--node-external);  stroke: var(--node-external-stroke);  stroke-width: 2; }
.pal-process { fill: var(--node-process); stroke: var(--node-process-stroke); stroke-width: 2; }
.pal-storage { fill: var(--node-storage); stroke: var(--node-storage-stroke); stroke-width: 2; }
.pal-flow  { stroke: var(--flow-stroke); stroke-width: 2.5; }
.pal-arrow { fill: var(--flow-stroke); }

/* ---------- canvas ---------- */
#canvas-wrap { grid-area: canvas; position: relative; overflow: hidden; }
#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: default;
  background: var(--bg);
}
#canvas.mode-flow .node { cursor: crosshair; }
.grid-dot { fill: var(--grid-dot); }
.arrow-head { fill: var(--flow-stroke); }

/* trust boundaries */
#boundaries-layer { pointer-events: none; }
.boundary-rect {
  fill: var(--accent-soft);
  fill-opacity: 0.35;
  stroke: var(--text-dim);
  stroke-width: 1.5;
  stroke-dasharray: 9 7;
}
.boundary-label {
  fill: var(--text-dim);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  user-select: none;
  /* Halo so a flow crossing the boundary never cuts through the text. */
  paint-order: stroke fill;
  stroke: var(--bg);
  stroke-width: 4px;
  stroke-linejoin: round;
}

/* nodes */
.node { cursor: grab; }
.node.dragging { cursor: grabbing; }
.node-shape { stroke-width: 2; }
.node-storage .node-shape { fill: var(--node-storage); stroke: var(--node-storage-stroke); }
.node-externalEntity  .node-shape { fill: var(--node-external);  stroke: var(--node-external-stroke); }
.node-process .node-shape { fill: var(--node-process); stroke: var(--node-process-stroke); }
.node-label {
  fill: var(--text);
  font-size: 13.5px;
  font-weight: 600;
  text-anchor: middle;
  user-select: none;
  pointer-events: none;
}
.node-type-label {
  fill: var(--text-dim);
  font-size: 9.5px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  text-anchor: middle;
  user-select: none;
  pointer-events: none;
}
/* Incompleteness warning (e.g. no trust zone): red outline + red pill.
   Must precede the .selected/.flow-source rules so selection wins. */
.node.warn .node-shape {
  stroke: var(--danger);
  stroke-dasharray: 7 4;
}
.node .warn .badge-pill-bg { stroke: var(--danger); }
.node .warn .badge-pill-text { fill: var(--danger); font-weight: 700; }

.node.selected .node-shape {
  stroke: var(--accent);
  stroke-width: 3;
  filter: drop-shadow(0 0 6px var(--accent));
}
.node.flow-source .node-shape {
  stroke: var(--accent);
  stroke-width: 3;
  animation: pulse 0.9s ease-in-out infinite alternate;
}
@keyframes pulse { from { stroke-opacity: 1; } to { stroke-opacity: 0.35; } }

/* node badges */
.badge-pill-bg { fill: var(--badge-bg); stroke: var(--border); rx: 8px; }
.badge-pill-text { fill: var(--text-dim); font-size: 10px; text-anchor: middle; user-select: none; pointer-events: none; }
.badge-icon { color: var(--ok); }
.badge-icon.warn { color: var(--warn); }

/* flows */
.flow-hit { fill: none; stroke: transparent; stroke-width: 18; cursor: pointer; }
.flow-line {
  fill: none;
  stroke: var(--flow-stroke);
  stroke-width: 2.2;
  pointer-events: none;
}
.flow.selected .flow-line { stroke: var(--accent); stroke-width: 3; }
.flow.selected .arrow-head { fill: var(--accent); }
.flow-label {
  fill: var(--text-dim);
  font-size: 11.5px;
  text-anchor: middle;
  user-select: none;
  pointer-events: none;
  paint-order: stroke fill;
  stroke: var(--bg);
  stroke-width: 4px;
  stroke-linejoin: round;
}
.flow-badges { cursor: pointer; }
.flow-badges .badge-letter {
  fill: currentColor;
  font-size: 8px;
  font-weight: 700;
  text-anchor: middle;
  user-select: none;
}
.flow-badges .badge-proto {
  fill: var(--text-dim);
  font-size: 9px;
  font-weight: 600;
  user-select: none;
}

/* rubber band */
.rubber-band {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-dasharray: 6 5;
  pointer-events: none;
}

/* ---------- properties panel ---------- */
#panel {
  grid-area: panel;
  width: 292px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  display: none;
}
#panel.open { display: block; }
#panel-handle { display: none; }
#panel-body { padding: 14px; }
#panel h2 {
  margin: 0 0 2px;
  font-size: 15px;
}
#panel .panel-subtitle {
  margin: 0 0 14px;
  color: var(--text-dim);
  font-size: 12px;
}
#panel .field { margin-bottom: 12px; }
#panel .field > label {
  display: block;
  margin-bottom: 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
}
#panel input[type="text"], #panel select, #panel textarea {
  width: 100%;
  padding: 7px 9px;
  min-height: 36px;
  font-size: 13.5px;
  font-family: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 7px;
}
#panel textarea { resize: vertical; min-height: 60px; }
#panel input[type="text"]:focus, #panel select:focus, #panel textarea:focus { border-color: var(--accent); outline: none; }
#panel .field-boolean { display: flex; align-items: center; gap: 8px; }
#panel .field-boolean label { margin: 0; font-weight: 600; color: var(--text); font-size: 13px; }
#panel input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }
#panel .radio-row { display: flex; gap: 6px; }
#panel .radio-row label {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 4px;
  border: 1px solid var(--border);
  border-radius: 7px;
  cursor: pointer;
  font-size: 13px;
}
#panel .radio-row input { display: none; }
#panel .radio-row label:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}
#panel .endpoints {
  padding: 8px 10px;
  margin-bottom: 14px;
  background: var(--surface-2);
  border-radius: 7px;
  font-size: 12.5px;
  color: var(--text-dim);
  word-break: break-word;
}
#panel .btn-delete {
  width: 100%;
  margin-top: 8px;
  padding: 9px;
  font-size: 13px;
  font-weight: 600;
  color: var(--danger);
  background: transparent;
  border: 1px solid var(--danger);
  border-radius: 7px;
  cursor: pointer;
}
#panel .btn-delete:hover { background: var(--danger); color: #fff; }
#panel .help { color: var(--text-dim); font-size: 12.5px; }

/* review list */
.review-item {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 100%;
  margin-bottom: 6px;
  padding: 8px 10px;
  text-align: left;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--text-dim);
  border-radius: 7px;
  cursor: pointer;
  font: inherit;
}
.review-item:hover { border-color: var(--accent); }
.review-item.sev-finding { border-left-color: var(--danger); }
.review-item.sev-inconsistent { border-left-color: var(--node-external-stroke); }
.review-item.sev-incomplete { border-left-color: var(--text-dim); }
.review-title { font-size: 12.5px; font-weight: 700; color: var(--text); }
.review-message { font-size: 12px; color: var(--text-dim); line-height: 1.35; }

#review-count {
  min-width: 17px;
  padding: 0 5px;
  border-radius: 9px;
  font-size: 11px;
  font-weight: 700;
  line-height: 17px;
  color: var(--bg);
  background: var(--text-dim);
}
#review-count:empty { display: none; }
#review-count.has-findings { background: var(--danger); }
#panel .section-title {
  margin: 16px 0 8px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
}

/* ---------- status bar ---------- */
#statusbar {
  grid-area: status;
  display: flex;
  gap: 12px;
  padding: 5px 12px;
  font-size: 12px;
  color: var(--text-dim);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
#status-hint { flex: 1; }
#status-warn { color: var(--danger); font-weight: 700; }

/* ---------- toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 44px;
  transform: translateX(-50%) translateY(16px);
  max-width: min(90vw, 420px);
  padding: 10px 16px;
  background: var(--text);
  color: var(--bg);
  border-radius: 9px;
  box-shadow: var(--shadow);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 30;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- mobile / narrow ---------- */
@media (max-width: 899px) {
  #app {
    grid-template-areas:
      "toolbar"
      "palette"
      "canvas"
      "status";
    grid-template-rows: auto auto 1fr auto;
    grid-template-columns: 1fr;
  }
  #toolbar { flex-wrap: nowrap; overflow-x: auto; }
  #toolbar .btn-text { display: none; }
  #diagram-title { max-width: 160px; }
  #palette {
    flex-direction: row;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 6px 8px;
  }
  .palette-btn { width: auto; min-width: 58px; padding: 5px 8px 4px; }
  #panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: 46dvh;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0;
    box-shadow: var(--shadow);
    z-index: 20;
  }
  #panel.open { display: block; }
  #panel-handle {
    display: flex;
    justify-content: flex-end;
    position: sticky;
    top: 0;
    padding: 6px 8px 0;
    background: var(--surface);
  }
  #panel-handle::before {
    content: "";
    position: absolute;
    left: 50%;
    top: 8px;
    width: 42px;
    height: 4px;
    border-radius: 2px;
    background: var(--border);
    transform: translateX(-50%);
  }
  #panel-close {
    width: 30px;
    height: 30px;
    border: none;
    border-radius: 50%;
    background: var(--surface-2);
    color: var(--text-dim);
    cursor: pointer;
  }
  #panel-body { padding: 10px 14px 18px; }
}
@media (min-width: 900px) {
  #panel-close { display: none; }
}

/* ---------- quick guide ---------- */

#btn-guide {
  min-width: 34px;
  font-weight: 700;
}

#guide {
  width: min(760px, calc(100vw - 32px));
  max-height: min(86vh, 860px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: hidden;
}
#guide::backdrop { background: rgba(12, 18, 28, 0.55); }
#guide:not([open]) { display: none; }

.guide-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-dim);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}
.guide-close:hover { color: var(--text); }

.guide-scroll {
  max-height: calc(min(86vh, 860px) - 62px);
  overflow-y: auto;
  padding: 22px 26px 8px;
}
#guide h2 {
  margin: 0 0 6px;
  font-size: 21px;
  letter-spacing: 0.2px;
}
.guide-lede {
  margin: 0 0 16px;
  color: var(--text-dim);
  font-size: 13.5px;
  line-height: 1.55;
  max-width: 62ch;
}

.guide-strip {
  display: block;
  width: 100%;
  height: auto;
  margin: 0 0 18px;
  padding: 12px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
}
.guide-strip .flow-line {
  stroke: var(--flow-stroke);
  stroke-width: 2.4;
  fill: none;
}
.guide-strip .arrow-head { fill: var(--flow-stroke); }
.guide-badge circle {
  fill: var(--badge-bg);
  stroke: var(--border);
  stroke-width: 1.5;
}
.guide-badge use { color: var(--ok); }

.guide-step { margin: 0 0 18px; }
.guide-step h3 {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 0 0 7px;
  font-size: 15px;
}
.guide-step-n {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
}
.guide-step p {
  margin: 0 0 8px;
  font-size: 13.5px;
  line-height: 1.6;
  max-width: 66ch;
}
.guide-step code {
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--surface-2);
  font-size: 12.5px;
}
.guide-note {
  padding: 10px 12px;
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  background: var(--accent-soft);
}
.guide-danger { color: var(--danger); }

.guide-types {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin: 12px 0 0;
  padding: 0;
  list-style: none;
}
.guide-type {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
}
.guide-type-art {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 68px;
}
.guide-type-art svg { height: 100%; width: auto; }
.guide-type h4 {
  margin: 0 0 4px;
  font-size: 13px;
}
.guide-type p {
  margin: 0;
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-dim);
}
.guide-eg {
  margin-top: 6px !important;
  font-style: italic;
}

.guide-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 26px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}
.guide-foot-hint {
  flex: 1;
  font-size: 12px;
  color: var(--text-dim);
}
.guide-foot button {
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.guide-foot button:hover { border-color: var(--accent); }
.guide-primary {
  border-color: var(--accent) !important;
  background: var(--accent) !important;
  color: #fff !important;
  font-weight: 600;
}

.guide-keys {
  display: grid;
  grid-template-columns: max-content 1fr;
  align-items: center;
  gap: 6px 14px;
  margin: 0;
  font-size: 13px;
}
.guide-keys dt { margin: 0; }
.guide-keys dd { margin: 0; color: var(--text-dim); }
.guide-keys kbd {
  display: inline-block;
  padding: 2px 7px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface-2);
  font-family: inherit;
  font-size: 12px;
  white-space: nowrap;
}
.guide-keys-block .guide-step-n { font-size: 11px; }

@media (max-width: 720px) {
  #guide {
    width: 100vw;
    max-width: 100vw;
    max-height: 100vh;
    height: 100vh;
    margin: 0;
    border: none;
    border-radius: 0;
  }
  .guide-scroll {
    max-height: calc(100vh - 62px);
    padding: 20px 16px 8px;
  }
  .guide-types { grid-template-columns: 1fr; }
  .guide-type { flex-direction: row; align-items: center; }
  .guide-type-art { width: 56px; flex: none; }
  .guide-foot { padding: 10px 16px; }
}
