:root {
  --bg: #f3f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #162033;
  --muted: #667085;
  --line: #dbe3ef;
  --primary: #1357d4;
  --primary-2: #0f43a8;
  --primary-soft: #eaf1ff;
  --success: #0f8a5f;
  --success-soft: #e8f7f0;
  --warning: #b76e00;
  --warning-soft: #fff5df;
  --danger: #c73d4d;
  --danger-soft: #ffedf0;
  --shadow: 0 12px 36px rgba(31, 55, 90, 0.08);
  --radius: 18px;
}

[data-theme="dark"] {
  --bg: #0d1421;
  --surface: #151f2f;
  --surface-2: #1b2739;
  --text: #f4f7fb;
  --muted: #9aa9bd;
  --line: #2a3a50;
  --primary: #74a7ff;
  --primary-2: #a9c8ff;
  --primary-soft: #1c3152;
  --success: #68d3a6;
  --success-soft: #17382d;
  --warning: #ffc868;
  --warning-soft: #47351b;
  --danger: #ff8795;
  --danger-soft: #48232a;
  --shadow: 0 16px 44px rgba(0, 0, 0, 0.24);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  min-height: 100vh;
  font-family: Inter, "Noto Sans Thai", "Segoe UI", Tahoma, sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top right, rgba(19, 87, 212, 0.09), transparent 34%),
    var(--bg);
}
button, input, select { font: inherit; }
button { cursor: pointer; }

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  max-width: 1600px;
  margin: 0 auto;
  padding: 28px 32px 18px;
}
.app-header h1 { margin: 2px 0 4px; font-size: clamp(1.7rem, 3vw, 2.5rem); letter-spacing: -0.04em; }
.eyebrow, .section-kicker { margin: 0; font-size: 0.72rem; font-weight: 800; letter-spacing: 0.16em; color: var(--primary); }
.subtitle, .muted { color: var(--muted); }
.subtitle { margin: 0; }
.header-actions { display: flex; gap: 8px; }

.app-shell {
  max-width: 1600px;
  margin: 0 auto;
  padding: 0 32px 40px;
  display: grid;
  grid-template-columns: minmax(300px, 360px) minmax(0, 1fr);
  gap: 24px;
  align-items: start;
}
.workspace { min-width: 0; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.sidebar { padding: 22px; position: sticky; top: 18px; }
.section-title-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.section-title-row.wrap { flex-wrap: wrap; }
.section-title-row h2, .chart-header h2, .export-card h2, .guide-card h2 { margin: 4px 0 0; font-size: 1.1rem; }
.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 7px 10px;
  font-size: 0.72rem;
  font-weight: 800;
  color: var(--primary);
  background: var(--primary-soft);
  white-space: nowrap;
}
.badge-alert { color: var(--danger); background: var(--danger-soft); }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 20px; }
.field { display: grid; gap: 7px; font-size: 0.82rem; color: var(--muted); }
.field-span-2 { grid-column: 1 / -1; }
input, select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 9px 11px;
  color: var(--text);
  background: var(--surface-2);
  outline: none;
  transition: border-color .18s, box-shadow .18s;
}
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }

.button-stack { display: grid; gap: 10px; margin-top: 18px; }
.button-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn {
  min-height: 40px;
  border-radius: 11px;
  border: 1px solid transparent;
  padding: 9px 14px;
  font-weight: 750;
  transition: transform .15s, background .15s, border-color .15s;
}
.btn:hover { transform: translateY(-1px); }
.btn-primary { color: #fff; background: var(--primary); }
.btn-primary:hover { background: var(--primary-2); }
.btn-secondary { color: var(--text); background: var(--surface-2); border-color: var(--line); }
.btn-secondary:hover, .btn-ghost:hover { border-color: var(--primary); }
.btn-ghost { color: var(--text); background: transparent; border-color: var(--line); }
.file-button { display: flex; align-items: center; justify-content: center; }
.file-button input { display: none; }
.help-box { margin-top: 16px; padding: 13px 14px; border-radius: 12px; background: var(--primary-soft); color: var(--text); }
.help-box strong { color: var(--primary); font-size: .82rem; }
.help-box p { margin: 6px 0 0; font-size: .78rem; line-height: 1.55; color: var(--muted); }

.tabs { display: flex; gap: 8px; overflow-x: auto; padding: 6px; margin-bottom: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); }
.tab { border: 0; background: transparent; color: var(--muted); border-radius: 10px; padding: 10px 16px; font-weight: 750; white-space: nowrap; }
.tab.active { background: var(--primary); color: #fff; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.metrics-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; margin-bottom: 14px; }
.metric-card { min-height: 118px; padding: 16px; border: 1px solid var(--line); border-radius: 15px; background: var(--surface); box-shadow: var(--shadow); }
.metric-card p { margin: 0; color: var(--muted); font-size: .75rem; }
.metric-card strong { display: block; margin: 9px 0 4px; font-size: 1.35rem; letter-spacing: -.03em; }
.metric-card span { color: var(--muted); font-size: .72rem; }
.status-stable { color: var(--success); }
.status-unstable { color: var(--danger); }

.chart-card { padding: 20px; margin-bottom: 14px; }
.chart-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; margin-bottom: 14px; }
.chart-header p { margin: 5px 0 0; }
.chart-wrap { position: relative; min-height: 320px; }
.chart-wrap.large { min-height: 430px; }
.legend-pills { display: flex; gap: 7px; flex-wrap: wrap; justify-content: flex-end; }
.legend-pill { padding: 5px 8px; border-radius: 999px; font-size: .68rem; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); }
.legend-pill.observed::before, .legend-pill.limit::before, .legend-pill.alert::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 5px; }
.legend-pill.observed::before { background: var(--primary); }
.legend-pill.limit::before { background: var(--warning); }
.legend-pill.alert::before { background: var(--danger); }
.two-column-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

.summary-card { padding: 20px; margin-bottom: 14px; }
.stats-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 10px; margin-top: 18px; }
.stat-item { padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); }
.stat-item span { display: block; color: var(--muted); font-size: .72rem; margin-bottom: 6px; }
.stat-item strong { font-size: 1rem; }
.capability-bars { display: grid; gap: 12px; margin-top: 18px; }
.cap-row { display: grid; grid-template-columns: 48px 1fr 58px; gap: 10px; align-items: center; font-size: .78rem; }
.cap-track { height: 10px; border-radius: 999px; overflow: hidden; background: var(--surface-2); border: 1px solid var(--line); }
.cap-fill { height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--danger), var(--warning), var(--success)); }
.summary-text { margin: 16px 0 0; color: var(--muted); line-height: 1.65; font-size: .85rem; }
.recommendation-list { display: grid; gap: 10px; margin-top: 18px; }
.recommendation-item { display: grid; grid-template-columns: 28px 1fr; gap: 10px; align-items: flex-start; padding: 11px 12px; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--line); }
.recommendation-item .icon { display: grid; place-items: center; width: 26px; height: 26px; border-radius: 50%; color: var(--primary); background: var(--primary-soft); font-weight: 900; }
.recommendation-item p { margin: 2px 0 0; color: var(--muted); font-size: .79rem; line-height: 1.55; }
.export-card { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 20px; }
.export-card p { margin: 6px 0 0; }
.export-actions { display: flex; gap: 9px; flex-wrap: wrap; }

.data-card { padding: 20px; }
.inline-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.table-scroll { overflow: auto; margin-top: 18px; border: 1px solid var(--line); border-radius: 13px; max-height: 680px; }
.data-table { width: 100%; border-collapse: collapse; min-width: 760px; }
.data-table th { position: sticky; top: 0; z-index: 1; background: var(--surface-2); color: var(--muted); font-size: .72rem; text-align: left; }
.data-table th, .data-table td { padding: 9px 10px; border-bottom: 1px solid var(--line); border-right: 1px solid var(--line); }
.data-table th:last-child, .data-table td:last-child { border-right: 0; }
.data-table tbody tr:last-child td { border-bottom: 0; }
.data-table input { min-width: 92px; min-height: 36px; padding: 7px 9px; background: transparent; }
.data-table .row-action { min-width: auto; width: 34px; padding: 4px; color: var(--danger); border-color: var(--line); background: transparent; }
.table-note { margin: 12px 0 0; color: var(--muted); font-size: .75rem; }

.guide-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.guide-card { padding: 22px; }
.guide-card.wide { grid-column: 1 / -1; }
.guide-card p { color: var(--muted); line-height: 1.75; }
.guide-list { display: grid; gap: 12px; margin-top: 18px; }
.guide-list div { display: grid; grid-template-columns: 72px 1fr; gap: 12px; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.guide-list.compact div { grid-template-columns: minmax(160px, .9fr) 1.1fr; }
.guide-list span { color: var(--muted); font-size: .82rem; }

.toast { position: fixed; right: 24px; bottom: 24px; z-index: 30; max-width: min(420px, calc(100vw - 48px)); padding: 12px 16px; border-radius: 12px; color: #fff; background: #202939; box-shadow: var(--shadow); opacity: 0; transform: translateY(12px); pointer-events: none; transition: opacity .2s, transform .2s; }
.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 1280px) {
  .metrics-grid { grid-template-columns: repeat(3, 1fr); }
  .stats-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 1000px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; }
}
@media (max-width: 720px) {
  .app-header, .app-shell { padding-left: 16px; padding-right: 16px; }
  .app-header { padding-top: 20px; }
  .metrics-grid { grid-template-columns: repeat(2, 1fr); }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .two-column-grid, .guide-grid { grid-template-columns: 1fr; }
  .guide-card.wide { grid-column: auto; }
  .chart-wrap.large { min-height: 350px; }
  .chart-header, .export-card { flex-direction: column; }
  .legend-pills { justify-content: flex-start; }
  .export-actions { width: 100%; }
  .export-actions .btn { flex: 1; }
}
@media (max-width: 470px) {
  .form-grid, .button-grid { grid-template-columns: 1fr; }
  .field-span-2 { grid-column: auto; }
  .metrics-grid { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr; }
  .app-header { align-items: center; }
}
