:root {
  --bg: #070b12;
  --panel: rgba(12, 18, 29, 0.9);
  --panel-strong: rgba(15, 24, 38, 0.98);
  --line: rgba(151, 170, 193, 0.16);
  --line-strong: rgba(113, 210, 255, 0.38);
  --text: #eef6ff;
  --muted: #91a1b5;
  --soft: #c9d7e8;
  --blue: #4aa3ff;
  --cyan: #4ddcff;
  --yellow: #ffd166;
  --red: #ff5f68;
  --green: #53df9e;
  --orange: #ff9c52;
  --shadow: 0 20px 70px rgba(0,0,0,0.36);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; min-height: 100%; }
body {
  overflow: hidden;
  font-family: Inter, Pretendard, "Noto Sans KR", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, sans-serif;
  background:
    linear-gradient(180deg, rgba(23, 35, 52, 0.66), rgba(7, 11, 18, 0.94) 38%),
    radial-gradient(circle at 18% 8%, rgba(77, 220, 255, 0.11), transparent 28%),
    radial-gradient(circle at 86% 14%, rgba(83, 223, 158, 0.08), transparent 30%),
    #070b12;
  color: var(--text);
}

button, input, textarea { font: inherit; }
button {
  cursor: pointer;
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 7px;
  padding: 9px 12px;
  background: linear-gradient(180deg, rgba(255,255,255,0.075), rgba(255,255,255,0.035));
  box-shadow: inset 0 1px rgba(255,255,255,0.07), 0 10px 24px rgba(0,0,0,0.18);
  transition: transform .16s ease, background .16s ease, border-color .16s ease, box-shadow .16s ease;
}
button:hover {
  transform: translateY(-1px);
  background: linear-gradient(180deg, rgba(255,255,255,0.11), rgba(255,255,255,0.052));
  border-color: var(--line-strong);
  box-shadow: inset 0 1px rgba(255,255,255,0.08), 0 14px 30px rgba(0,0,0,0.22);
}
button.primary {
  border-color: rgba(77,220,255,0.54);
  color: #f7fdff;
  background: linear-gradient(135deg, rgba(77, 220, 255, 0.31), rgba(83, 223, 158, 0.18));
}
button.secondary, button.icon-button { color: #cfe0f4; }
button.wide { width: 100%; }

textarea {
  width: 100%;
  min-height: 90px;
  resize: vertical;
  border: 1px solid rgba(155,185,218,.22);
  border-radius: 7px;
  padding: 11px;
  color: var(--text);
  background: rgba(5, 11, 19, .58);
  outline: none;
  line-height: 1.45;
}
textarea:focus {
  border-color: rgba(77,220,255,.58);
  box-shadow: 0 0 0 3px rgba(77,220,255,.1);
}

.app-shell {
  display: grid;
  grid-template-columns: 320px minmax(540px, 1fr) 372px;
  grid-template-rows: 76px minmax(0, 1fr) 188px;
  height: 100vh;
  gap: 10px;
  padding: 10px;
}

.topbar {
  grid-column: 1 / 4;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: linear-gradient(180deg, rgba(18, 28, 43, 0.94), rgba(10, 17, 28, 0.91));
  box-shadow: var(--shadow), inset 0 1px rgba(255,255,255,0.07);
  backdrop-filter: blur(18px);
}
.brand-block { display: flex; align-items: center; gap: 12px; min-width: 290px; }
.logo-mark {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-weight: 900;
  color: #06111d;
  background: linear-gradient(135deg, #4ddcff, #53df9e 74%);
  box-shadow: 0 0 26px rgba(77,220,255,0.25), inset 0 1px rgba(255,255,255,0.42);
}
h1 { margin: 0; font-size: 20px; letter-spacing: 0; }
.brand-block p { margin: 3px 0 0; color: var(--muted); font-size: 12px; }
.top-stats { display: flex; gap: 8px; align-items: center; justify-content: center; flex: 1; min-width: 0; }
.stat-chip {
  min-width: 124px;
  padding: 8px 10px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.04);
}
.stat-chip span { display: block; color: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: .08em; }
.stat-chip strong { display: block; margin-top: 2px; font-size: 13px; }
.stat-chip.success strong { color: var(--green); }
.stat-chip.warning strong { color: var(--yellow); }
.top-actions { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; justify-content: flex-end; }
.top-actions button { white-space: nowrap; padding: 8px 9px; }

.panel {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  box-shadow: var(--shadow), inset 0 1px rgba(255,255,255,0.045);
  backdrop-filter: blur(18px);
  min-width: 0;
}
.left-panel, .right-panel {
  overflow: auto;
  padding: 12px;
  scrollbar-width: thin;
  scrollbar-color: rgba(145,161,181,.38) transparent;
}
.left-panel { grid-column: 1; grid-row: 2 / 4; }
.right-panel { grid-column: 3; grid-row: 2 / 4; }
.panel-section {
  padding: 12px;
  border: 1px solid rgba(155,185,218,0.16);
  border-radius: 9px;
  background: linear-gradient(180deg, rgba(255,255,255,0.045), rgba(255,255,255,0.024));
  margin-bottom: 10px;
}
.panel-section.compact { padding: 11px; }
.panel-title { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 9px; gap: 8px; }
.panel-title span { font-weight: 800; letter-spacing: 0; color: #f7fbff; }
.panel-title small { color: var(--muted); font-size: 11px; white-space: nowrap; }

.incident-list { display: grid; gap: 8px; }
.incident-card {
  width: 100%;
  text-align: left;
  padding: 11px;
  border-radius: 9px;
  background: linear-gradient(180deg, rgba(255,255,255,.055), rgba(255,255,255,.028));
}
.incident-card b { display: block; color: #fff; font-size: 13px; line-height: 1.28; }
.incident-card span { display: block; margin-top: 4px; color: var(--muted); font-size: 12px; }
.incident-card.selected {
  border-color: rgba(77,220,255,.58);
  background: linear-gradient(135deg, rgba(77,220,255,.18), rgba(83,223,158,.09));
  box-shadow: inset 3px 0 rgba(77,220,255,.82), 0 14px 26px rgba(0,0,0,.18);
}
.incident-card.critical b { color: #ffd7da; }
.incident-card.warning b { color: #ffedbd; }
.input-actions { margin-top: 8px; }

.kb-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.kb-grid div, .incident-summary div {
  min-width: 0;
  padding: 9px;
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 9px;
  background: rgba(5, 11, 19, 0.42);
}
.kb-grid span, .incident-summary span, .sync-grid span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  margin-bottom: 4px;
}
.kb-grid b, .incident-summary b, .sync-grid b { font-size: 13px; color: #fff; font-weight: 800; }
.ready { color: var(--green) !important; }
.danger-text { color: var(--red) !important; }

.layer-row {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 0;
  color: #dcecff;
  font-size: 13px;
  border-bottom: 1px solid rgba(255,255,255,0.055);
}
.layer-row:last-child { border-bottom: none; }
.layer-row input { accent-color: var(--cyan); }

.viewport-panel { grid-column: 2; grid-row: 2; min-width: 0; min-height: 0; position: relative; }
.scene-container {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid rgba(130, 174, 226, 0.26);
  background: radial-gradient(circle at 50% 44%, #17263a 0%, #080d15 74%);
  box-shadow: 0 24px 90px rgba(0,0,0,0.42), inset 0 1px rgba(255,255,255,0.07);
}
#scene3d { display: block; width: 100%; height: 100%; }
.viewport-hud {
  position: absolute;
  z-index: 4;
  border: 1px solid rgba(155,185,218,.2);
  border-radius: 9px;
  background: rgba(7, 13, 22, 0.76);
  backdrop-filter: blur(10px);
  color: #d9ebff;
  box-shadow: 0 12px 40px rgba(0,0,0,0.25);
}
.viewport-hud.top-left { top: 12px; left: 12px; max-width: min(520px, calc(100% - 318px)); padding: 10px 12px; }
.viewport-hud.top-right { top: 12px; right: 12px; max-width: 260px; padding: 10px 12px; text-align: right; }
.viewport-hud.top-left strong, .viewport-hud.top-right strong { display: block; margin-bottom: 4px; font-size: 13px; line-height: 1.35; }
.viewport-hud.top-left span, .viewport-hud.top-right span { color: var(--muted); font-size: 12px; line-height: 1.35; }
.viewport-hud.bottom-left { left: 12px; bottom: 12px; display: flex; gap: 8px; padding: 7px; font-size: 12px; }
.viewport-hud.bottom-left span { padding: 6px 8px; border-radius: 8px; background: rgba(255,255,255,0.07); }
.scan-overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: none;
  place-items: center;
  background: rgba(6, 13, 24, 0.56);
  backdrop-filter: blur(4px);
}
.scan-overlay.active { display: grid; }
.scan-card {
  display: grid;
  justify-items: center;
  gap: 9px;
  max-width: 390px;
  padding: 24px;
  border-radius: 10px;
  border: 1px solid rgba(77,220,255,0.3);
  background: rgba(12,24,42,0.9);
  text-align: center;
}
.scan-card span { color: var(--muted); font-size: 13px; line-height: 1.45; }
.loader-ring {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 3px solid rgba(77,220,255,.18);
  border-top-color: var(--cyan);
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.incident-summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.risk-strip {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-top: 9px;
  padding: 9px 10px;
  border: 1px solid rgba(255,95,104,.22);
  border-radius: 9px;
  background: rgba(255,95,104,.08);
}
.risk-strip span { color: var(--muted); font-size: 12px; }
.risk-strip b { color: var(--red); }
.cause-text { margin: 0 0 9px; color: #dcecff; font-size: 13px; line-height: 1.55; }
.rag-list { display: grid; gap: 7px; }
.rag-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 8px;
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 9px;
  background: rgba(255,255,255,.035);
}
.rag-item span { color: #dcecff; font-size: 12px; line-height: 1.35; }
.rag-item b { color: var(--green); font-size: 12px; }
.sync-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 7px 11px;
  align-items: baseline;
}
.step-list {
  margin: 0;
  padding-left: 21px;
  color: #dcecff;
  font-size: 13px;
  line-height: 1.55;
}
.step-list li { margin: 0 0 6px; }
.metric-row { display: flex; justify-content: space-between; gap: 12px; margin-top: 10px; color: #dcecff; font-size: 13px; }
.metric-row b { color: #fff; text-align: right; }
.bar {
  width: 100%;
  height: 7px;
  background: rgba(255,255,255,0.08);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 6px;
}
.bar i { display: block; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--cyan), var(--green)); transition: width .35s ease; }
.bar.risk i { background: linear-gradient(90deg, var(--yellow), var(--red)); }

.bottom-panel { grid-column: 2; grid-row: 3; padding: 12px; overflow: hidden; }
.bottom-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 10px; }
.bottom-header strong { display: block; font-size: 15px; }
.bottom-header span { color: var(--muted); font-size: 12px; }
.time-chip { padding: 7px 10px; border-radius: 9px; color: var(--cyan); background: rgba(77,220,255,.09); border: 1px solid rgba(77,220,255,.2); white-space: nowrap; }
.pipeline {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 9px;
}
.pipe-step {
  min-width: 0;
  padding: 9px;
  border-radius: 9px;
  border: 1px solid rgba(255,255,255,0.08);
  background: linear-gradient(180deg, rgba(255,255,255,0.052), rgba(255,255,255,0.026));
}
.pipe-step b { display: block; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pipe-step span { display: block; margin-top: 3px; color: var(--muted); font-size: 10px; text-transform: uppercase; }
.pipe-step.active { border-color: rgba(77,220,255,.56); background: rgba(77,220,255,.13); box-shadow: inset 0 2px rgba(77,220,255,.44); }
.pipe-step.done { border-color: rgba(83,223,158,.34); background: rgba(83,223,158,.08); }
.analysis-log {
  display: grid;
  gap: 4px;
  max-height: 72px;
  overflow: auto;
  padding-right: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  color: #cfe0f4;
}
.analysis-log div {
  padding: 4px 7px;
  border-left: 2px solid rgba(77,220,255,.45);
  background: rgba(255,255,255,.035);
}

.world-label {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 5;
  pointer-events: none;
  min-width: 76px;
  padding: 5px 7px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.2);
  background: rgba(11, 24, 39, 0.86);
  color: #e8f5ff;
  font-size: 10px;
  font-weight: 800;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
  box-shadow: 0 10px 28px rgba(0,0,0,.28), inset 0 1px rgba(255,255,255,.08);
  backdrop-filter: blur(10px);
}
.world-label span { display: block; }
.world-label.small {
  min-width: auto;
  padding: 3px 6px;
  border-radius: 6px;
  font-size: 10px;
  color: #9db9d6;
  background: rgba(10, 18, 30, 0.58);
}
.world-label.danger {
  color: #ffddb7;
  background: rgba(67, 33, 43, 0.9);
  border-color: rgba(255, 156, 82, 0.32);
}
.world-label.sync {
  color: #d5fff1;
  border-color: rgba(83,223,158,.35);
}

.report-modal {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: none;
  place-items: center;
  padding: 24px;
  background: rgba(4, 9, 16, .68);
  backdrop-filter: blur(8px);
}
.report-modal.active { display: grid; }
.report-card {
  width: min(720px, 100%);
  max-height: min(740px, 92vh);
  overflow: hidden;
  border: 1px solid rgba(155,185,218,.24);
  border-radius: 10px;
  background: var(--panel-strong);
  box-shadow: 0 24px 90px rgba(0,0,0,.42);
}
.report-header, .report-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid rgba(255,255,255,.08);
}
.report-footer { border-top: 1px solid rgba(255,255,255,.08); border-bottom: none; justify-content: flex-end; }
.report-header span { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; }
.report-header h2 { margin: 2px 0 0; font-size: 18px; }
.report-card pre {
  margin: 0;
  padding: 16px;
  max-height: 520px;
  overflow: auto;
  white-space: pre-wrap;
  color: #e7f2ff;
  font: 13px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (max-width: 1260px) {
  .app-shell {
    grid-template-columns: 300px minmax(420px, 1fr);
    grid-template-rows: auto minmax(520px, 1fr) 220px auto;
    height: auto;
    min-height: 100vh;
    overflow: auto;
  }
  body { overflow: auto; }
  .topbar { grid-column: 1 / 3; align-items: flex-start; flex-wrap: wrap; }
  .top-stats { justify-content: flex-start; order: 3; width: 100%; }
  .left-panel { grid-column: 1; grid-row: 2 / 4; }
  .viewport-panel { grid-column: 2; grid-row: 2; min-height: 520px; }
  .bottom-panel { grid-column: 2; grid-row: 3; }
  .right-panel { grid-column: 1 / 3; grid-row: 4; }
  .pipeline { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 820px) {
  .app-shell { display: block; padding: 10px; }
  .topbar, .panel, .viewport-panel { margin-bottom: 10px; }
  .viewport-panel { height: 520px; }
  .top-actions, .top-stats { width: 100%; justify-content: flex-start; }
  .pipeline { grid-template-columns: 1fr 1fr; }
  .incident-summary, .kb-grid { grid-template-columns: 1fr; }
  .viewport-hud.top-left { max-width: calc(100% - 24px); }
  .viewport-hud.top-right { top: auto; bottom: 58px; right: 12px; text-align: left; }
}
