/* 스마트 밸브 제어시스템 2.0 — 시제품 목업 스타일
   색상 토큰은 검증된 데이터 시각화 팔레트를 사용한다.
   - 상태색(good/warning/serious/critical)은 고정 팔레트이며, 항상 점 + 텍스트
     라벨을 함께 표기해 색상 단독으로 의미를 전달하지 않는다.
   - 진행률 미터는 단일 blue 램프(sequential). */

:root {
  color-scheme: light;

  --surface-1: #fcfcfb;      /* 카드 표면 */
  --page: #f9f9f7;           /* 페이지 평면 */
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);

  /* sequential blue — 진행률 미터 */
  --meter-track: #cde2fb;
  --meter-fill: #2a78d6;
  --accent: #2a78d6;
  --accent-ink: #ffffff;

  /* status (고정, 테마 무관) */
  --st-good: #0ca30c;
  --st-warning: #fab219;
  --st-serious: #ec835a;
  --st-critical: #d03b3b;
  --st-idle: #898781;

  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.05);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --page: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --meter-track: #184f95;
    --meter-fill: #3987e5;
    --accent: #3987e5;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --page: #0d0d0d;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --gridline: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --meter-track: #184f95;
  --meter-fill: #3987e5;
  --accent: #3987e5;
  --shadow: none;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", "Apple SD Gothic Neo", sans-serif;
  font-size: 14px;
  line-height: 1.6;
}

h1, h2, h3 { margin: 0; font-weight: 650; }
h1 { font-size: 15px; }
h2 { font-size: 17px; }
h3 { font-size: 14px; }

.muted { color: var(--text-muted); font-size: 12px; font-weight: 400; }

/* --- 헤더 --------------------------------------------------------------- */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 12px; }

.brand-mark {
  width: 30px; height: 30px;
  border-radius: 8px;
  background: var(--accent);
  flex: none;
  position: relative;
}
.brand-mark::after {
  content: "";
  position: absolute; inset: 10px;
  border-radius: 50%;
  border: 2px solid var(--accent-ink);
  border-right-color: transparent;
}

.brand-sub { margin: 0; font-size: 11px; color: var(--text-muted); }

.header-right { display: flex; align-items: center; gap: 10px; }

.conn-badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11px;
  color: var(--text-secondary);
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
}
.conn-badge::before {
  content: "";
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--st-idle);
}
.conn-badge.is-on::before { background: var(--st-good); }
.conn-badge.is-off::before { background: var(--st-critical); }

/* --- 탭 ----------------------------------------------------------------- */

.tabs {
  display: flex;
  gap: 2px;
  padding: 0 20px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}

.tab {
  appearance: none;
  border: 0;
  background: none;
  padding: 11px 14px;
  font: inherit;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}
.tab:hover { color: var(--text-primary); }
.tab.is-active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

main { padding: 20px; max-width: 1180px; margin: 0 auto; }

.view { display: none; }
.view.is-active { display: block; }

.section-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 12px; margin-bottom: 14px;
}

/* --- 카드 --------------------------------------------------------------- */

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
  overflow: hidden;
}

.card-head {
  padding: 12px 16px;
  border-bottom: 1px solid var(--gridline);
}

.two-col {
  display: grid;
  grid-template-columns: minmax(320px, 420px) 1fr;
  gap: 16px;
  align-items: start;
}

/* --- KPI 스탯 타일 ------------------------------------------------------ */

.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat-tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-label { font-size: 12px; color: var(--text-secondary); }

.stat-value {
  font-size: 30px;
  font-weight: 650;
  letter-spacing: -0.02em;
  color: var(--text-primary);
}
.stat-value small {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
  margin-left: 3px;
}

.stat-note { font-size: 11px; color: var(--text-muted); }

/* --- 테이블 ------------------------------------------------------------- */

.data-table { width: 100%; border-collapse: collapse; }

.data-table th {
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  padding: 9px 16px;
  border-bottom: 1px solid var(--gridline);
  white-space: nowrap;
}

.data-table td {
  padding: 11px 16px;
  border-bottom: 1px solid var(--gridline);
  font-variant-numeric: tabular-nums;
  vertical-align: middle;
}
.data-table tr:last-child td { border-bottom: 0; }
.data-table .col-wide { width: 42%; }

.empty-row td {
  color: var(--text-muted);
  text-align: center;
  padding: 26px 16px;
}

/* --- 상태 배지 (점 + 라벨) --------------------------------------------- */

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
}
.badge::before {
  content: "";
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--st-idle);
  flex: none;
}
.badge[data-status="running"]::before { background: var(--st-good); }
.badge[data-status="warning"]::before { background: var(--st-critical); }
.badge[data-status="maintenance"]::before { background: var(--st-serious); }
.badge[data-status="idle"]::before { background: var(--st-idle); }
.badge[data-status="stale"]::before { background: var(--st-warning); }

/* --- 진행률 미터 -------------------------------------------------------- */

.meter {
  display: flex; align-items: center; gap: 10px;
}
.meter-track {
  flex: 1;
  height: 8px;
  min-width: 80px;
  background: var(--meter-track);
  border-radius: 4px;
  overflow: hidden;
}
.meter-fill {
  height: 100%;
  background: var(--meter-fill);
  border-radius: 4px;
  transition: width 0.5s ease;
}
.meter-value {
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
  min-width: 34px;
  text-align: right;
}

/* --- 채팅 --------------------------------------------------------------- */

.chat-shell {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  height: calc(100vh - 190px);
  min-height: 460px;
  overflow: hidden;
}

.chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--gridline);
  font-size: 13px;
}

.pill {
  font-size: 11px;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 9px;
}

.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg { display: flex; }
.msg.user { justify-content: flex-end; }

/* 말풍선 폭 제한은 래퍼가 갖는다. .bubble에 %  max-width를 주면 플렉스
   아이템 폭과 서로를 참조해 실제보다 좁게 계산된다. */
.msg-body { max-width: min(72%, 620px); }

.bubble {
  padding: 10px 14px;
  border-radius: 14px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 13.5px;
  line-height: 1.65;
}

.msg.assistant .bubble {
  background: var(--page);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg.user .bubble {
  background: var(--accent);
  color: var(--accent-ink);
  border-bottom-right-radius: 4px;
}

.msg-meta {
  font-size: 10.5px;
  color: var(--text-muted);
  margin-top: 4px;
}

.typing { display: inline-flex; gap: 4px; padding: 4px 2px; }
.typing span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-muted);
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 60%, 100% { opacity: 0.25; } 30% { opacity: 1; } }

.quick-bar {
  display: flex;
  gap: 8px;
  padding: 10px 16px 0;
  flex-wrap: wrap;
}
.quick-bar button {
  appearance: none;
  font: inherit;
  font-size: 12px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  cursor: pointer;
}
.quick-bar button:hover { border-color: var(--accent); color: var(--accent); }

.chat-input {
  display: flex;
  gap: 8px;
  padding: 12px 16px 14px;
}
.chat-input input {
  flex: 1;
  font: inherit;
  padding: 10px 13px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--baseline);
  background: var(--surface-1);
  color: var(--text-primary);
}
.chat-input input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

.retry-btn {
  appearance: none; font: inherit; font-size: 12px;
  margin-top: 8px; padding: 5px 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-1); color: var(--text-primary); cursor: pointer;
}

/* --- 버튼 --------------------------------------------------------------- */

.primary-btn, .ghost-btn {
  appearance: none;
  font: inherit;
  font-size: 13px;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.primary-btn {
  background: var(--accent);
  color: var(--accent-ink);
  border: 1px solid var(--accent);
  font-weight: 600;
}
.primary-btn:disabled { opacity: 0.5; cursor: default; }
.ghost-btn {
  background: var(--surface-1);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

/* --- 폼 ----------------------------------------------------------------- */

.form { padding: 16px; display: flex; flex-direction: column; gap: 14px; }

.form label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--text-secondary); }
.form label em { color: var(--st-critical); font-style: normal; }

.form input, .form select, .form textarea {
  font: inherit;
  font-size: 13px;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--baseline);
  background: var(--surface-1);
  color: var(--text-primary);
  width: 100%;
}
.form textarea { resize: vertical; }
.form input:focus, .form select:focus, .form textarea:focus {
  outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent;
}
.form .invalid { border-color: var(--st-critical); }

.hint { font-size: 11px; color: var(--text-muted); }

.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 2px; }

.feedback {
  padding: 11px 13px;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  border: 1px solid var(--border);
  background: var(--page);
  white-space: pre-wrap;
}
.feedback .feedback-title { display: flex; align-items: center; gap: 7px; font-weight: 600; color: var(--text-primary); }
.feedback .feedback-title::before { content: ""; width: 8px; height: 8px; border-radius: 50%; }
.feedback[data-kind="success"] .feedback-title::before { background: var(--st-good); }
.feedback[data-kind="warning"] .feedback-title::before { background: var(--st-warning); }
.feedback[data-kind="error"] .feedback-title::before { background: var(--st-critical); }

/* --- 모니터링 그리드 ---------------------------------------------------- */

.extractor-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.extractor-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-left: 3px solid var(--st-idle);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 13px 15px;
  display: flex;
  flex-direction: column;
  gap: 9px;
}
.extractor-card[data-status="running"] { border-left-color: var(--st-good); }
.extractor-card[data-status="warning"] { border-left-color: var(--st-critical); }
.extractor-card[data-status="maintenance"] { border-left-color: var(--st-serious); }

.extractor-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.extractor-name { font-weight: 650; font-size: 13.5px; }

.readouts { display: flex; gap: 16px; }
.readout { display: flex; flex-direction: column; }
.readout-label { font-size: 10.5px; color: var(--text-muted); }
.readout-value { font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; }
.readout-value.is-over { color: var(--st-critical); }

.extractor-foot {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 11px; color: var(--text-muted);
  border-top: 1px solid var(--gridline);
  padding-top: 8px;
}

.stale-badge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11px; color: var(--text-secondary);
}
.stale-badge::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%;
  background: var(--st-warning);
}

/* --- 알림 목록 ---------------------------------------------------------- */

.alert-list { list-style: none; margin: 0; padding: 0; }

.alert-item {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 16px;
  border-bottom: 1px solid var(--gridline);
}
.alert-item:last-child { border-bottom: 0; }

.alert-sev {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11px; font-weight: 600;
  color: var(--text-secondary);
  white-space: nowrap;
  min-width: 58px;
}
.alert-sev::before { content: ""; width: 8px; height: 8px; border-radius: 50%; }
.alert-sev[data-sev="danger"]::before { background: var(--st-critical); }
.alert-sev[data-sev="warning"]::before { background: var(--st-warning); }
.alert-sev[data-sev="info"]::before { background: var(--st-idle); }

.alert-msg { flex: 1; font-size: 13px; }
.alert-time { font-size: 11px; color: var(--text-muted); font-variant-numeric: tabular-nums; }

.empty-note { padding: 26px 16px; text-align: center; color: var(--text-muted); font-size: 13px; }

@media (max-width: 860px) {
  .two-col { grid-template-columns: 1fr; }
  main { padding: 14px; }
  .msg-body { max-width: 88%; }
}
