:root {
  --primary: #1B4EF5;
  --secondary: #3874FF;
  --accent: #5996FF;
  --soft-purple: #F4CEFF;
  --ink: #172033;
  --muted: #68738a;
  --surface: #ffffff;
  --surface-soft: #f6f8fc;
  --line: #e2e8f2;
  --success: #17885f;
  --warning: #b36b00;
  --danger: #bd2f44;
  --sidebar-width: 272px;
  --shadow: 0 14px 40px rgba(21, 55, 126, .09);
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI Variable Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; color-scheme: light; }
body {
  margin: 0;
  color: var(--ink);
  background: var(--surface-soft);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.55;
  letter-spacing: -.006em;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
button, input, select, textarea { font: inherit; }
button, .btn, .form-control, .form-select { letter-spacing: 0; }
h1, h2, h3, h4, h5, h6 { font-family: var(--font-ui); line-height: 1.2; text-wrap: balance; }
p, li, td, th { text-wrap: pretty; }

.app-shell { min-height: 100vh; display: flex; }
.app-sidebar {
  width: var(--sidebar-width);
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 1040;
  display: flex;
  flex-direction: column;
  color: #fff;
  background:
    radial-gradient(circle at 12% 16%, rgba(244, 206, 255, .25), transparent 23%),
    linear-gradient(165deg, #1238bb 0%, var(--primary) 50%, #0d2c95 100%);
  box-shadow: 10px 0 35px rgba(27, 78, 245, .16);
}
.brand-block { display: flex; align-items: center; gap: 12px; padding: 24px 20px; border-bottom: 1px solid rgba(255,255,255,.15); }
.brand-mark, .login-logo {
  width: 44px; height: 44px; border-radius: 14px; display: grid; place-items: center;
  font-weight: 700; font-size: 22px; background: var(--soft-purple); color: var(--primary);
  box-shadow: 0 8px 25px rgba(0,0,0,.13);
}
.brand-title { font-weight: 700; letter-spacing: -.015em; }
.brand-subtitle { color: rgba(255,255,255,.72); font-size: 12px; }
.app-nav { display: grid; gap: 7px; padding: 18px 12px; }
.nav-item {
  border: 0; width: 100%; color: rgba(255,255,255,.8); background: transparent;
  text-align: left; padding: 12px 14px; border-radius: 13px; display: flex; gap: 12px; align-items: center;
  transition: .2s ease; font-size: 14px; font-weight: 600;
}
.nav-item:hover { color: #fff; background: rgba(255,255,255,.1); transform: translateX(2px); }
.nav-item.active { color: var(--primary); background: #fff; box-shadow: 0 12px 30px rgba(3, 18, 75, .2); }
.nav-icon { width: 20px; text-align: center; font-size: 18px; }
.sidebar-footer { margin-top: auto; padding: 18px 20px 24px; color: rgba(255,255,255,.72); border-top: 1px solid rgba(255,255,255,.15); }
.lineage-status { display: flex; align-items: center; gap: 8px; font-size: 13px; color: #fff; margin-bottom: 5px; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: #5ce0aa; box-shadow: 0 0 0 5px rgba(92,224,170,.13); }
.sidebar-close { display: none; margin-left: auto; color: #fff; }
.sidebar-backdrop { display: none; position: fixed; inset: 0; z-index: 1035; background: rgba(12, 24, 55, .55); }

.app-main { width: calc(100% - var(--sidebar-width)); margin-left: var(--sidebar-width); min-height: 100vh; display: flex; flex-direction: column; }
.app-header {
  position: sticky; top: 0; z-index: 1020; min-height: 84px; display: flex; align-items: center; justify-content: space-between; gap: 20px;
  padding: 15px 30px; background: rgba(255,255,255,.92); backdrop-filter: blur(16px); border-bottom: 1px solid var(--line);
}
.header-left { display: flex; align-items: center; gap: 14px; }
.header-left h1 { margin: 0; font-size: clamp(21px, 2.2vw, 28px); letter-spacing: -.022em; font-weight: 700; }
.header-left p { margin: 4px 0 0; color: var(--muted); font-size: 13.5px; line-height: 1.45; }
.header-actions { display: flex; align-items: center; gap: 12px; }
.menu-button { display: none; }
.icon-button { border: 0; background: transparent; width: 40px; height: 40px; border-radius: 10px; font-size: 22px; }
.icon-button:hover { background: rgba(255,255,255,.13); }
.soft-badge { display: inline-flex; padding: 7px 11px; border-radius: 999px; background: #edf3ff; color: var(--primary); font-size: 12px; font-weight: 700; }

.app-content { flex: 1; padding: 28px 30px 50px; max-width: 1700px; width: 100%; margin: 0 auto; }
.app-footer { display: flex; justify-content: space-between; gap: 20px; padding: 15px 30px; color: var(--muted); border-top: 1px solid var(--line); font-size: 12px; }
.loading-state { min-height: 48vh; display: grid; place-content: center; justify-items: center; gap: 12px; color: var(--muted); }

.page-hero {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; margin-bottom: 22px;
  padding: 22px 24px; border: 1px solid #dbe6ff; border-radius: 20px; background:
    radial-gradient(circle at 92% 15%, rgba(244,206,255,.72), transparent 25%),
    linear-gradient(110deg, #fff 15%, #edf3ff 100%);
}
.page-hero h2 { margin: 0 0 8px; font-size: 23px; font-weight: 700; letter-spacing: -.018em; }
.page-hero p { margin: 0; color: var(--muted); max-width: 900px; line-height: 1.62; }
.hero-meta { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.kpi-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 22px; }
.kpi-card, .panel, .analysis-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow);
}
.kpi-card { padding: 18px; position: relative; overflow: hidden; }
.kpi-card::after { content: ""; position: absolute; width: 70px; height: 70px; border-radius: 50%; right: -24px; top: -22px; background: var(--soft-purple); opacity: .5; }
.kpi-label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .055em; }
.kpi-value { font-size: 29px; font-weight: 700; letter-spacing: -.025em; margin-top: 7px; font-variant-numeric: tabular-nums; }
.kpi-note { font-size: 12.5px; color: var(--secondary); font-weight: 600; margin-top: 4px; }

.panel { padding: 20px; margin-bottom: 20px; }
.panel-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 14px; margin-bottom: 16px; }
.panel-title { margin: 0; font-size: 18px; font-weight: 700; letter-spacing: -.012em; }
.panel-subtitle { color: var(--muted); margin: 5px 0 0; font-size: 13.5px; line-height: 1.5; }
.panel-grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.panel-grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px; }
.chart-box { min-height: 330px; position: relative; }
.chart-box.compact { min-height: 270px; }
.chart-box canvas { max-height: 430px; }

.finding-list { display: grid; gap: 14px; }
.finding-card { border: 1px solid var(--line); border-left: 5px solid var(--primary); border-radius: 15px; padding: 16px 18px; background: #fff; }
.finding-card h3 { margin: 0 0 10px; font-size: 16px; font-weight: 700; letter-spacing: -.008em; }
.finding-detail { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.finding-detail div { padding: 11px 13px; background: var(--surface-soft); border-radius: 11px; font-size: 13px; }
.finding-detail strong { display: block; color: var(--primary); margin-bottom: 4px; }
.warning-list, .recommendation-list { margin: 0; padding-left: 20px; display: grid; gap: 9px; }
.warning-list li::marker { color: var(--warning); }
.recommendation-list li::marker { color: var(--primary); }

.control-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; }
.form-label { color: var(--ink); font-size: 13.5px; font-weight: 600; margin-bottom: 7px; }
.form-control, .form-select { border-color: #ced8e9; border-radius: 11px; font-size: 14px; line-height: 1.45; }
.form-control:focus, .form-select:focus { border-color: var(--accent); box-shadow: 0 0 0 .2rem rgba(89,150,255,.17); }
.btn { font-size: 14px; font-weight: 600; line-height: 1.4; }
.table th { font-weight: 600; letter-spacing: .005em; }
.table td { font-weight: 400; }
.filter-panel summary { cursor: pointer; list-style: none; font-weight: 700; display: flex; align-items: center; justify-content: space-between; letter-spacing: -.008em; }
.filter-panel summary::-webkit-details-marker { display: none; }
.filter-panel summary::after { content: "⌄"; color: var(--primary); font-size: 20px; }
.filter-panel[open] summary::after { transform: rotate(180deg); }
.filter-panel-body { margin-top: 16px; }
.multi-select { min-height: 110px; }
.active-filters { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.filter-chip { background: #eaf1ff; color: #24459f; border: 1px solid #cfddff; border-radius: 999px; padding: 6px 10px; font-size: 12px; font-weight: 600; }

.table-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 15px; flex-wrap: wrap; margin-bottom: 12px; }
.table-wrap { border: 1px solid var(--line); border-radius: 14px; overflow: auto; max-height: 68vh; background: #fff; }
.data-table { margin: 0; min-width: 1500px; font-size: 13px; line-height: 1.45; font-variant-numeric: tabular-nums; }
.data-table th { position: sticky; top: 0; z-index: 2; background: #eaf1ff; color: #17387e; white-space: nowrap; border-bottom: 2px solid #bfd0ff; }
.data-table td { white-space: nowrap; max-width: 280px; overflow: hidden; text-overflow: ellipsis; }
.data-table tbody tr:hover td { background: #f7f9ff; }
.blank-pill { color: #7b6682; background: #f8eaff; padding: 2px 7px; border-radius: 999px; font-size: 11px; }
.value-pill { display: inline-flex; padding: 3px 8px; border-radius: 999px; background: #e9f0ff; color: #224aaf; font-weight: 700; }
.value-pill.no { background: #eef1f5; color: #596272; }
.pagination-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 14px; flex-wrap: wrap; }
.pagination-buttons { display: flex; align-items: center; gap: 8px; }

.analysis-layout { display: grid; grid-template-columns: 290px minmax(0, 1fr); gap: 18px; }
.analysis-sidebar { position: sticky; top: 105px; align-self: start; max-height: calc(100vh - 130px); overflow: auto; }
.variable-list { display: grid; gap: 6px; margin-top: 12px; }
.variable-button { border: 1px solid transparent; background: transparent; text-align: left; border-radius: 10px; padding: 9px 10px; color: var(--muted); font-size: 13px; }
.variable-button:hover { background: #f1f5ff; color: var(--primary); }
.variable-button.active { background: #e7efff; border-color: #bed0ff; color: #183f9f; font-weight: 600; }
.metric-strip { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; margin-bottom: 18px; }
.metric-mini { padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-soft); }
.metric-mini span { display: block; color: var(--muted); font-size: 11px; }
.metric-mini strong { display: block; margin-top: 4px; font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.flag-row { display: flex; flex-wrap: wrap; gap: 7px; }
.flag { display: inline-flex; padding: 5px 8px; border-radius: 999px; background: var(--soft-purple); color: #6c3279; font-size: 11px; font-weight: 600; letter-spacing: .01em; }
.insight-box { border-radius: 14px; padding: 15px; background: linear-gradient(110deg, #edf3ff, #fff7ff); border: 1px solid #d8e2ff; }
.insight-box strong { color: var(--primary); }
.frequency-table { font-size: 13px; }
.frequency-table th { color: #28477e; background: #eef3ff; }

.stat-grid { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 12px; margin-bottom: 16px; }
.stat-card { padding: 14px; border: 1px solid var(--line); border-radius: 13px; background: var(--surface-soft); }
.stat-card span { color: var(--muted); font-size: 11px; display: block; }
.stat-card strong { display: block; margin-top: 4px; font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-card small { display: block; color: var(--muted); margin-top: 3px; }
.cross-table { min-width: 620px; font-size: 13px; }
.cross-table th { background: #eaf1ff; }
.cross-table .total-cell { font-weight: 700; background: #f4f6fa; }
.definition-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 12px; }
.definition-card { border-radius: 13px; padding: 14px; border: 1px solid #d8e4ff; background: #f7f9ff; }
.definition-card h4 { font-size: 14px; font-weight: 700; color: var(--primary); letter-spacing: -.006em; }
.definition-card p { margin: 0; color: var(--muted); font-size: 12px; }

.fallback-chart { display: grid; gap: 8px; padding: 10px 2px; }
.fallback-row { display: grid; grid-template-columns: minmax(120px, 220px) 1fr 60px; gap: 10px; align-items: center; font-size: 12px; }
.fallback-track { height: 13px; background: #edf1f8; border-radius: 999px; overflow: hidden; }
.fallback-bar { height: 100%; border-radius: 999px; background: linear-gradient(90deg, var(--primary), var(--accent)); }

.app-toast { position: fixed; z-index: 2000; right: 20px; bottom: 20px; max-width: 360px; padding: 12px 16px; border-radius: 12px; background: #172033; color: #fff; box-shadow: var(--shadow); opacity: 0; pointer-events: none; transform: translateY(10px); transition: .2s; }
.app-toast.show { opacity: 1; transform: translateY(0); }
.empty-state { padding: 45px 20px; text-align: center; color: var(--muted); }
.error-state { border: 1px solid #f1b6c0; background: #fff0f2; color: #8e2135; border-radius: 15px; padding: 18px; }

.login-page { min-height: 100vh; background: #f5f7fb; }
.login-shell { min-height: 100vh; display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(420px, .85fr); }
.login-visual { padding: clamp(42px, 8vw, 110px); color: #fff; display: flex; flex-direction: column; justify-content: center; background:
  radial-gradient(circle at 80% 20%, rgba(244,206,255,.32), transparent 24%),
  radial-gradient(circle at 10% 80%, rgba(89,150,255,.35), transparent 28%),
  linear-gradient(145deg, #0c2b99, var(--primary) 65%, #4f78ff); }
.login-visual .login-logo { margin-bottom: 34px; }
.login-visual h1 { max-width: 720px; font-size: clamp(35px, 5vw, 62px); line-height: 1.08; letter-spacing: -.035em; font-weight: 700; }
.login-visual > p:not(.eyebrow) { max-width: 640px; color: rgba(255,255,255,.82); font-size: 17px; line-height: 1.65; }
.eyebrow { font-size: 11px; font-weight: 700; letter-spacing: .14em; color: var(--accent); }
.login-visual .eyebrow { color: var(--soft-purple); }
.login-feature-list { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 26px; font-size: 13px; color: rgba(255,255,255,.86); }
.login-card-wrap { display: grid; place-items: center; padding: 30px; }
.login-card { width: min(440px, 100%); padding: 34px; background: #fff; border: 1px solid var(--line); border-radius: 22px; box-shadow: var(--shadow); }
.login-card h2 { font-size: 31px; font-weight: 700; letter-spacing: -.025em; }
.login-card-heading > p:last-child { color: var(--muted); }
.login-note { margin: 18px 0 0; color: var(--muted); font-size: 12px; line-height: 1.5; text-align: center; }

@media (max-width: 1200px) {
  .kpi-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .control-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .panel-grid-3 { grid-template-columns: 1fr 1fr; }
  .metric-strip { grid-template-columns: repeat(3, minmax(0,1fr)); }
}

@media (max-width: 900px) {
  .app-sidebar { transform: translateX(-105%); transition: transform .25s ease; }
  body.sidebar-open .app-sidebar { transform: translateX(0); }
  body.sidebar-open .sidebar-backdrop { display: block; }
  .sidebar-close, .menu-button { display: grid; place-items: center; }
  .app-main { width: 100%; margin-left: 0; }
  .app-header { padding: 14px 18px; }
  .app-content { padding: 22px 18px 40px; }
  .app-footer { padding: 14px 18px; }
  .analysis-layout { grid-template-columns: 1fr; }
  .analysis-sidebar { position: static; max-height: none; }
  .variable-list { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .login-shell { grid-template-columns: 1fr; }
  .login-visual { min-height: 44vh; padding: 45px 28px; }
  .login-visual h1 { font-size: 38px; }
  .login-card-wrap { padding: 25px 18px 50px; }
}

@media (max-width: 680px) {
  .header-actions .soft-badge { display: none; }
  .header-left p { display: none; }
  .page-hero { flex-direction: column; padding: 18px; }
  .hero-meta { justify-content: flex-start; }
  .kpi-grid, .panel-grid-2, .panel-grid-3, .control-grid, .definition-grid { grid-template-columns: 1fr; }
  .metric-strip, .stat-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .finding-detail { grid-template-columns: 1fr; }
  .variable-list { grid-template-columns: 1fr; }
  .app-footer { flex-direction: column; gap: 4px; }
  .fallback-row { grid-template-columns: 100px 1fr 45px; }
  .login-card { padding: 25px 20px; }
}
