:root {
  --bg-deep: #090e14;
  --bg-panel: #101822;
  --bg-card: #15202e;
  --bg-card-hover: #1b293b;
  --border: #25364a;
  --border-bright: #36506e;
  --text-main: #e2ebf5;
  --text-dim: #8ca0b8;
  --ice: #5cc8e6;
  --ice-soft: rgba(92, 200, 230, 0.14);
  --amber: #e6a64c;
  --amber-soft: rgba(230, 166, 76, 0.14);
  --crimson: #e35353;
  --crimson-soft: rgba(227, 83, 83, 0.16);
  --green: #4ec78c;
  --font-sans: "PingFang SC", "Noto Sans SC", "Microsoft YaHei", system-ui, sans-serif;
  --font-serif: "Songti SC", "Noto Serif SC", Georgia, serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg-deep);
  color: var(--text-main);
  font-family: var(--font-sans);
  font-size: 13px;
  line-height: 1.5;
  overflow: hidden;
  height: 100vh;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* --- TOP HUD --- */
.top-hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px;
  background: linear-gradient(180deg, #111b27 0%, #0d141e 100%);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.hud-left, .hud-metrics, .hud-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.3px;
}

.badge-loop {
  background: var(--ice-soft);
  color: var(--ice);
  border: 1px solid rgba(92, 200, 230, 0.35);
}

.badge-order.order-intact { background: rgba(78, 199, 140, 0.14); color: var(--green); border: 1px solid rgba(78, 199, 140, 0.35); }
.badge-order.order-fraying { background: var(--amber-soft); color: var(--amber); border: 1px solid rgba(230, 166, 76, 0.35); }
.badge-order.order-collapsed,
.badge-order.order-jungle { background: var(--crimson-soft); color: var(--crimson); border: 1px solid rgba(227, 83, 83, 0.45); }

.clock-block { line-height: 1.2; }
.clock-date { font-size: 14px; font-weight: 700; color: #f0f6fc; }
.clock-sub { font-size: 11px; color: var(--amber); }

.hud-metrics {
  background: rgba(9, 14, 20, 0.65);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  gap: 14px;
}

.metric { display: flex; flex-direction: column; }
.metric-label { font-size: 10px; color: var(--text-dim); }
.metric-val { font-size: 12.5px; font-weight: 600; }
.metric-val.ice { color: var(--ice); }
.metric-val.amber { color: var(--amber); }
.metric-val.danger { color: var(--crimson); }

.view-switch {
  display: flex;
  background: #0a1017;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px;
}

button {
  cursor: pointer;
  font-family: inherit;
  border: none;
  border-radius: 5px;
  transition: background 0.15s, border-color 0.15s, transform 0.08s;
}
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.42; cursor: not-allowed; }

.btn-tab {
  background: transparent;
  color: var(--text-dim);
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 600;
}
.btn-tab.active { background: #1e3147; color: #fff; }

.btn-primary {
  background: linear-gradient(180deg, #277da1 0%, #1d5d7a 100%);
  color: #f3fbff;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid #3a96be;
}
.btn-primary:hover { filter: brightness(1.12); }

.btn-accent {
  background: linear-gradient(180deg, #b87a28 0%, #8f5c1a 100%);
  color: #fff8eb;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid #d99b43;
}
.btn-accent:hover { filter: brightness(1.12); }

.btn-ghost {
  background: #141f2d;
  color: var(--text-main);
  border: 1px solid var(--border);
  padding: 5px 10px;
  font-size: 12px;
}
.btn-ghost:hover, .btn-ghost.active { border-color: var(--ice); color: var(--ice); }

.btn-danger {
  background: var(--crimson-soft);
  color: #ff9494;
  border: 1px solid rgba(227, 83, 83, 0.5);
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
}
.btn-danger:hover { background: rgba(227, 83, 83, 0.28); }

.btn-danger-solid {
  background: linear-gradient(180deg, #c93b3b 0%, #992626 100%);
  color: #fff;
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 700;
  border: 1px solid #f06565;
}

.llm-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11.5px;
  color: var(--text-dim);
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: #0d1520;
  cursor: pointer;
}

/* --- MAIN WORKSPACE --- */
.workspace-grid {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 370px;
  min-height: 0;
}
.workspace-grid.dossier-collapsed {
  grid-template-columns: 1fr 0px;
}
.workspace-grid.dossier-collapsed .dossier-drawer {
  display: none;
}

.primary-stage {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: radial-gradient(circle at 30% 10%, #121f2e 0%, #090e14 75%);
}

.stage-view {
  display: none;
  flex: 1;
  min-height: 0;
  flex-direction: column;
}
.stage-view.active { display: flex; }

/* --- VIEW 1: MAP --- */
.map-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  background: rgba(13, 21, 31, 0.92);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.layer-group, .time-actions { display: flex; align-items: center; gap: 6px; }
.toolbar-title { font-size: 11px; color: var(--text-dim); margin-right: 4px; }

.layer-btn {
  background: #131e2c;
  color: var(--text-dim);
  border: 1px solid var(--border);
  padding: 4px 10px;
  font-size: 11.5px;
}
.layer-btn.active {
  background: var(--ice-soft);
  color: var(--ice);
  border-color: var(--ice);
}

.map-canvas-wrap {
  position: relative;
  height: 44%;
  min-height: 255px;
  background: #0a1119;
  border-bottom: 1px solid var(--border);
  overflow: hidden;
}

.map-svg-container {
  width: 100%;
  height: 100%;
}
.map-svg-container svg {
  width: 100%;
  height: 100%;
  display: block;
}

.map-overlay-tag {
  position: absolute;
  left: 12px;
  bottom: 10px;
  background: rgba(9, 14, 20, 0.8);
  border: 1px solid var(--border);
  padding: 3px 9px;
  border-radius: 4px;
  font-size: 11px;
  color: var(--text-dim);
}

.map-node { cursor: pointer; transition: filter 0.15s; }
.map-node:hover { filter: brightness(1.22); }

/* --- LOCATION ACTION CONSOLE --- */
.location-console {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
}

.loc-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px 6px;
}
.loc-header h2 { font-size: 15px; color: #f0f6fc; }
.loc-desc { font-size: 11.5px; color: var(--text-dim); margin-top: 2px; }

.loc-grids { display: flex; gap: 6px; flex-wrap: wrap; }
.grid-pill {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: #0d1520;
}
.grid-pill.ok { color: var(--green); border-color: rgba(78, 199, 140, 0.35); }
.grid-pill.warn { color: var(--amber); border-color: rgba(230, 166, 76, 0.45); }
.grid-pill.fail { color: var(--crimson); border-color: rgba(227, 83, 83, 0.5); }

.console-tabs {
  display: flex;
  gap: 4px;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
}
.console-tab {
  background: transparent;
  color: var(--text-dim);
  padding: 7px 11px;
  font-size: 12px;
  border-radius: 5px 5px 0 0;
  border-bottom: 2px solid transparent;
}
.console-tab.active {
  color: var(--ice);
  border-bottom-color: var(--ice);
  background: rgba(92, 200, 230, 0.08);
}

.console-body {
  flex: 1;
  overflow-y: auto;
  padding: 10px 14px 14px;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(245px, 1fr));
  gap: 8px;
}

.action-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 9px 10px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 6px;
}
.action-card:hover { border-color: var(--border-bright); }
.action-card-title { font-size: 12.5px; font-weight: 600; color: #f0f6fc; display: flex; justify-content: space-between; gap: 6px; }
.action-card-meta { font-size: 11px; color: var(--text-dim); }
.action-card-btns { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 2px; }
.btn-mini {
  background: #1d2d40;
  color: #dbe8f6;
  border: 1px solid #304762;
  padding: 3px 8px;
  font-size: 11px;
}
.btn-mini:hover { border-color: var(--ice); color: var(--ice); }
.btn-mini.warn { border-color: rgba(230, 166, 76, 0.5); color: var(--amber); }
.btn-mini.danger { border-color: rgba(227, 83, 83, 0.5); color: #ff8e8e; }

.sub-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 10px;
  padding: 7px 10px;
  background: #0d1520;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 11.5px;
}
.sub-bar select, .sub-bar input {
  background: #142030;
  color: var(--text-main);
  border: 1px solid var(--border-bright);
  border-radius: 4px;
  padding: 3px 7px;
  font-size: 11.5px;
}

/* --- VIEW 2: NOVEL / PERFORMANCE STAGE --- */
.scene-stage {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.scene-backdrop {
  position: absolute;
  inset: 0;
  z-index: 1;
}
.scene-backdrop svg,
.real-scene-img { width: 100%; height: 100%; object-fit: cover; display: block; }

.real-portrait-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: bottom center;
  display: block;
}

.scene-filter {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  background: linear-gradient(180deg, rgba(8, 13, 20, 0.35) 0%, rgba(8, 13, 20, 0.88) 78%);
  transition: background 0.4s;
}
.scene-filter.freezing {
  box-shadow: inset 0 0 90px rgba(92, 200, 230, 0.28);
}
.scene-filter.danger-alert {
  box-shadow: inset 0 0 110px rgba(227, 83, 83, 0.36);
}
.scene-filter.blackout {
  background: linear-gradient(180deg, rgba(3, 6, 10, 0.72) 0%, rgba(4, 7, 12, 0.95) 80%);
}

.scene-foreground {
  position: relative;
  z-index: 3;
  height: 100%;
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 16px;
  padding: 18px;
}

.portrait-zone {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
}

.portrait-card {
  background: rgba(15, 24, 35, 0.88);
  border: 1px solid var(--border-bright);
  border-radius: 8px;
  padding: 12px;
  text-align: center;
  backdrop-filter: blur(6px);
}
.portrait-svg-wrap {
  width: 196px;
  height: 215px;
  margin: 0 auto 8px;
  border-radius: 6px;
  background: radial-gradient(circle at 50% 35%, #1d2d42 0%, #0b1118 100%);
  border: 1px solid var(--border);
  overflow: hidden;
}
.portrait-name { font-size: 16px; font-weight: 700; color: #fff; }
.portrait-role { font-size: 11.5px; color: var(--ice); margin-bottom: 6px; }
.surface-tags { display: flex; justify-content: center; gap: 5px; flex-wrap: wrap; }
.tag-chip {
  font-size: 10.5px;
  padding: 2px 6px;
  border-radius: 3px;
  background: #1b2a3c;
  color: var(--text-dim);
  border: 1px solid var(--border);
}

/* Signature Loop-2+ Crimson Sticky-Note Annotations */
.red-stickies { display: flex; flex-direction: column; gap: 7px; }
.red-sticky {
  background: rgba(65, 16, 20, 0.92);
  border: 1px solid rgba(227, 83, 83, 0.75);
  border-left: 4px solid var(--crimson);
  border-radius: 5px;
  padding: 8px 10px;
  font-size: 11.5px;
  color: #ffd5d5;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  transform: rotate(-0.6deg);
}
.red-sticky strong { color: #ff7b7b; display: block; margin-bottom: 2px; }

.narrative-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: rgba(11, 18, 26, 0.9);
  border: 1px solid var(--border-bright);
  border-radius: 8px;
  backdrop-filter: blur(8px);
  overflow: hidden;
}

.narrative-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: rgba(16, 26, 38, 0.9);
}
.story-loc-badge { font-weight: 700; color: var(--ice); font-size: 13px; }
.story-event-tag { font-size: 11px; color: var(--text-dim); }

.active-event-box {
  padding: 14px 16px;
  border-bottom: 1px solid rgba(227, 83, 83, 0.4);
  background: linear-gradient(180deg, rgba(46, 18, 22, 0.55) 0%, rgba(16, 24, 34, 0.85) 100%);
  max-height: 62%;
  overflow-y: auto;
}
.evt-title { font-size: 16px; color: #ff9c9c; margin-bottom: 8px; font-family: var(--font-serif); }
.evt-dialogue {
  font-family: var(--font-serif);
  font-size: 14.5px;
  line-height: 1.75;
  color: #f4f7fb;
  white-space: pre-wrap;
  margin-bottom: 10px;
}
.evt-microcue {
  font-size: 12px;
  font-style: italic;
  color: #f2c98a;
  background: rgba(230, 166, 76, 0.1);
  border-left: 3px solid var(--amber);
  padding: 6px 10px;
  margin-bottom: 10px;
  border-radius: 0 4px 4px 0;
}

.evt-options {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 7px;
  margin-bottom: 10px;
}
.evt-opt-btn {
  text-align: left;
  padding: 8px 10px;
  background: #172434;
  border: 1px solid #324a66;
  color: #ebf3fc;
  font-size: 12px;
}
.evt-opt-btn:hover:not(:disabled) { border-color: var(--ice); background: #1e3147; }
.evt-opt-btn small { display: block; color: var(--text-dim); font-size: 10.5px; margin-top: 2px; }

.l3-box {
  border-top: 1px dashed var(--border-bright);
  padding-top: 8px;
}
.l3-header { font-size: 11px; color: var(--ice); margin-bottom: 5px; }
.l3-input-row { display: flex; gap: 6px; }
.l3-input-row input {
  flex: 1;
  background: #0c141d;
  border: 1px solid var(--border-bright);
  border-radius: 5px;
  color: #fff;
  padding: 7px 10px;
  font-size: 12.5px;
}

.log-stream {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  font-family: var(--font-serif);
  font-size: 13.5px;
  line-height: 1.7;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.log-line {
  padding: 6px 10px;
  border-radius: 4px;
  background: rgba(20, 30, 44, 0.55);
  border-left: 2px solid #2b425c;
}
.log-line.critical { border-left-color: var(--crimson); background: rgba(65, 20, 24, 0.35); color: #ffd4d4; }
.log-line.butterfly { border-left-color: var(--amber); background: rgba(66, 47, 16, 0.35); color: #ffe5b4; }
.log-line.ice { border-left-color: var(--ice); color: #d5f4ff; }

.story-footer-bar {
  display: flex;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  background: rgba(13, 21, 31, 0.95);
}

/* --- RIGHT COLLAPSIBLE DOSSIER --- */
.dossier-drawer {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: #0d141d;
  border-left: 1px solid var(--border);
}

.dossier-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.dossier-head h3 { font-size: 13.5px; color: var(--ice); }
.seed-pill { font-size: 10.5px; color: var(--text-dim); background: #152130; padding: 2px 7px; border-radius: 4px; border: 1px solid var(--border); }

.dossier-tabs {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  border-bottom: 1px solid var(--border);
}
.dtab {
  background: transparent;
  color: var(--text-dim);
  padding: 7px 2px;
  font-size: 11.5px;
  border-radius: 0;
  border-bottom: 2px solid transparent;
}
.dtab.active { color: var(--ice); border-bottom-color: var(--ice); background: rgba(92, 200, 230, 0.08); }

.dossier-content {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.dcard {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
}
.dcard-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.dcard-title { font-weight: 700; font-size: 12.5px; color: #f0f6fc; }
.dcard-sub { font-size: 11px; color: var(--text-dim); }
.dcard.has-red { border-color: rgba(227, 83, 83, 0.55); }

.stat-bar-row { display: flex; align-items: center; gap: 8px; margin: 4px 0; font-size: 11.5px; }
.stat-bar-track { flex: 1; height: 6px; background: #0a1017; border-radius: 3px; overflow: hidden; }
.stat-bar-fill { height: 100%; background: var(--ice); }

/* --- MODALS & CHRONICLE READER --- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(4, 8, 13, 0.84);
  backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.hidden { display: none !important; }

.modal-card {
  background: #101924;
  border: 1px solid var(--border-bright);
  border-radius: 10px;
  width: min(820px, 95vw);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.7);
}
.modal-card.small-card { width: min(500px, 94vw); }

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: #142030;
}
.modal-header h2 { font-size: 17px; font-family: var(--font-serif); margin-top: 4px; }

.chron-body {
  flex: 1;
  overflow-y: auto;
  padding: 18px 22px;
  font-family: var(--font-serif);
  font-size: 14.5px;
  line-height: 1.85;
  white-space: pre-wrap;
  color: #e9eff7;
}
.chron-red {
  display: block;
  margin: 10px 0;
  padding: 10px 14px;
  background: rgba(88, 16, 22, 0.52);
  border: 1px solid rgba(227, 83, 83, 0.6);
  border-left: 4px solid var(--crimson);
  border-radius: 5px;
  color: #ffd3d3;
}

.chron-unlocks {
  padding: 10px 18px;
  border-top: 1px solid var(--border);
  background: #0d1520;
  font-size: 12px;
  color: var(--amber);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  background: #121d2b;
}

.form-stack {
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.form-stack label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--text-dim); }
.form-stack input[type="number"], .form-stack select {
  background: #0a1119;
  color: #fff;
  border: 1px solid var(--border-bright);
  border-radius: 5px;
  padding: 8px 10px;
  font-size: 13px;
}
.form-stack label.inline-check { flex-direction: row; align-items: center; gap: 8px; color: var(--text-main); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  z-index: 120;
  background: #18283b;
  color: #f0f8ff;
  border: 1px solid var(--ice);
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 12.5px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.55);
}

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: #0a1017; }
::-webkit-scrollbar-thumb { background: #26394e; border-radius: 3px; }
