:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1f2733;
  --muted: #6b7785;
  --line: #dfe3e8;
  --accent: #2d5d7c;
  --red: #e05656;
  --yellow: #e7b53c;
  --green: #4caf72;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--red); min-height: 1.1em; font-size: 0.85rem; }

button {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  padding: 0.45rem 0.8rem;
  border-radius: 6px;
}
button.ghost {
  background: transparent;
  color: var(--accent);
}
button.danger { border-color: var(--red); background: var(--red); }
button:disabled { opacity: 0.5; cursor: default; }

input, select, textarea {
  font: inherit;
  padding: 0.45rem 0.55rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
}

/* ---------- gate ---------- */
.gate {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  background: var(--bg);
}
.gate-card {
  background: var(--panel);
  padding: 2rem;
  border-radius: 10px;
  box-shadow: var(--shadow);
  width: min(380px, 92vw);
}
.gate-card h1 { margin: 0 0 1rem; color: var(--accent); }
.gate-card input, .gate-card select { width: 100%; margin-bottom: 0.6rem; }
.gate-card button { width: 100%; }
.gate-card .row { display: flex; gap: 0.5rem; }
.gate-card .row input { flex: 1; }
.gate-card .row button { width: auto; white-space: nowrap; }

/* ---------- topbar ---------- */
.topbar {
  display: flex; align-items: center; gap: 1rem;
  padding: 0.6rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; color: var(--accent); }
.legend { display: flex; gap: 0.4rem; flex: 1; }
.chip { font-size: 0.72rem; padding: 0.15rem 0.5rem; border-radius: 999px; color: #fff; }
.chip.red { background: var(--red); }
.chip.yellow { background: var(--yellow); color: #4a3a00; }
.chip.green { background: var(--green); }
.topbar-right { display: flex; align-items: center; gap: 0.6rem; }

/* ---------- stats bar ---------- */
.statsbar {
  display: flex; align-items: center; gap: 1rem;
  padding: 0.4rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.statbar-track {
  flex: 1; display: flex; height: 14px;
  border-radius: 7px; overflow: hidden; background: var(--line);
}
.statbar-seg { height: 100%; transition: width 0.2s; }
.statbar-seg.red { background: var(--red); }
.statbar-seg.yellow { background: var(--yellow); }
.statbar-seg.green { background: var(--green); }
.statbar-legend { display: flex; gap: 1rem; font-size: 0.8rem; white-space: nowrap; }
.statbar-legend b { font-variant-numeric: tabular-nums; }

/* ---------- layout (resizable) ---------- */
.app { display: flex; flex-direction: column; height: 100vh; }
.layout {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 6px var(--detail-w, 560px);
}
.tree { overflow-y: auto; padding: 1rem; }
.splitter { background: var(--line); cursor: col-resize; }
.splitter:hover, .splitter.dragging { background: var(--accent); }
.detail {
  border-left: 1px solid var(--line);
  background: var(--panel);
  overflow-y: auto;
  padding: 1rem;
}
.detail-empty { margin-top: 2rem; text-align: center; }

/* ---------- tree ---------- */
.volume { margin-bottom: 1rem; }
.node-head {
  display: flex; align-items: center; gap: 0.5rem;
  cursor: pointer;
  user-select: none;
}
.caret { width: 1rem; color: var(--muted); transition: transform 0.1s; }
.caret.collapsed { transform: rotate(-90deg); }
.dot { width: 12px; height: 12px; border-radius: 3px; flex: none; }
.dot.red { background: var(--red); }
.dot.yellow { background: var(--yellow); }
.dot.green { background: var(--green); }

.volume > .node-head {
  background: var(--accent); color: #fff;
  padding: 0.5rem 0.75rem; border-radius: 6px;
  font-weight: 700; font-size: 1.05rem;
}
.volume > .node-head .dot { box-shadow: 0 0 0 2px rgba(255,255,255,0.6); }

.episode { margin: 0.5rem 0 0.5rem 1.25rem; }
.episode > .node-head {
  background: #e8edf1; color: var(--accent);
  padding: 0.4rem 0.6rem; border-radius: 6px; font-weight: 600;
}
.node-actions { margin-left: auto; display: flex; gap: 0.3rem; }
.node-actions button {
  padding: 0.15rem 0.45rem; font-size: 0.75rem;
  background: transparent; color: inherit; border-color: currentColor; opacity: 0.8;
}

.pages {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
  gap: 3px 4px;
  padding: 0.4rem 0 0.3rem 1.5rem;
}
.page-cell { display: flex; flex-direction: column; align-items: stretch; gap: 1px; }
.page-num { font-size: 0.62rem; color: var(--muted); text-align: center; line-height: 1.1; }
.square {
  position: relative;
  width: 100%; height: 18px;
  border-radius: 3px; cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 2px;
  color: #fff; font-size: 0.56rem; font-weight: 700; line-height: 1;
  border: 1px solid rgba(0,0,0,0.12);
}
.square.script { border-bottom: none; }
.square.red { background: var(--red); }
.square.yellow { background: var(--yellow); color: #4a3a00; }
.square.green { background: var(--green); }
.square.selected { outline: 2px solid var(--accent); outline-offset: 1px; z-index: 1; }
.square .stars { font-size: 0.5rem; opacity: 0.95; letter-spacing: -1px; }
.square .alert {
  position: absolute; top: -5px; right: -5px;
  background: #fff; color: var(--red); border-radius: 50%;
  width: 13px; height: 13px; font-size: 9px; line-height: 13px;
  text-align: center; box-shadow: var(--shadow); border: 1px solid var(--red);
}
.track-label { font-size: 0.6rem; color: var(--muted); width: 100%; }

/* ---------- detail pane ---------- */
.detail h2 { margin: 0 0 0.25rem; }
.detail .sub { color: var(--muted); font-size: 0.85rem; margin-bottom: 0.75rem; }
.detail textarea { width: 100%; min-height: 60vh; resize: vertical; font-family: ui-monospace, Menlo, monospace; line-height: 1.5; }
.detail .art-img { max-width: 100%; border: 1px solid var(--line); border-radius: 6px; }
.detail-actions { display: flex; gap: 0.5rem; align-items: center; margin: 0.75rem 0; flex-wrap: wrap; }
.star-box { display: flex; align-items: center; gap: 0.5rem; margin: 0.5rem 0; }
.star-btn.on { background: var(--yellow); border-color: var(--yellow); color: #4a3a00; }
.alert-banner {
  background: #fff4f4; border: 1px solid var(--red); color: #a13030;
  padding: 0.5rem 0.7rem; border-radius: 6px; margin-bottom: 0.75rem;
  display: flex; justify-content: space-between; align-items: center; gap: 0.5rem;
}
.saved-note { color: var(--green); font-size: 0.85rem; }
