/* ── Reset & Variables ────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
  --primary:     #2563eb;
  --primary-dk:  #1d4ed8;
  --primary-lt:  #dbeafe;
  --bg:          #f1f5f9;
  --surface:     #ffffff;
  --text:        #1e293b;
  --text-2:      #64748b;
  --border:      #e2e8f0;
  --green:       #16a34a;
  --green-lt:    #dcfce7;
  --red:         #dc2626;
  --red-lt:      #fee2e2;
  --amber:       #f59e0b;
  --amber-lt:    #fef3c7;
  --radius:      8px;
  --shadow:      0 1px 3px rgba(0,0,0,.08);
  --shadow-lg:   0 4px 12px rgba(0,0,0,.10);
}

html { font-size: 15px; scroll-behavior: smooth; }
body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Header ──────────────────────────────────────────────────────── */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 50;
}
.header-inner {
  max-width: 1440px; margin: 0 auto;
  padding: .6rem 1.5rem;
  display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap;
}
.logo {
  font-size: 1.15rem; font-weight: 700; display: flex; align-items: center; gap: .55rem;
  color: #111; white-space: nowrap;
}
.logo-img {
  height: 28px; width: 28px; object-fit: contain; flex-shrink: 0; border-radius: 4px;
}

.main-nav { display: flex; gap: .35rem; flex-wrap: wrap; }
.nav-btn {
  background: none; border: none; padding: .4rem .9rem; border-radius: var(--radius);
  font: inherit; font-weight: 500; color: var(--text-2); cursor: pointer;
  transition: background .15s, color .15s;
}
.nav-btn:hover:not(:disabled) { background: var(--bg); color: var(--text); }
.nav-btn.active { background: var(--primary-lt); color: var(--primary-dk); }
.nav-btn:disabled { opacity: .4; cursor: not-allowed; }

/* ── Main content ────────────────────────────────────────────────── */
.app-main {
  max-width: 1440px; width: 100%; margin: 0 auto;
  padding: 1.5rem; flex: 1;
}
.section-header { margin-bottom: 1.25rem; }
.section-header h2 { font-size: 1.35rem; font-weight: 700; }
.section-header p  { color: var(--text-2); margin-top: .25rem; }

/* ── Buttons ─────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .5rem 1.1rem; border-radius: var(--radius);
  font: inherit; font-weight: 600; font-size: .9rem;
  background: var(--primary); color: #fff; border: none;
  cursor: pointer; transition: background .15s;
}
.btn:hover { background: var(--primary-dk); }
.btn-sm { padding: .35rem .8rem; font-size: .82rem; }
.btn-outline {
  background: var(--surface); color: var(--primary); border: 1.5px solid var(--primary);
}
.btn-outline:hover { background: var(--primary-lt); }

/* ── Data Snapshot Card ──────────────────────────────────────────── */
.snapshot-card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow-lg); padding: 1.5rem;
  border: 1px solid var(--border); margin-bottom: 1.5rem;
}
.snapshot-header {
  display: flex; align-items: baseline; justify-content: space-between;
  flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem;
  border-bottom: 2px solid var(--primary-lt); padding-bottom: .75rem;
}
.snapshot-header h3 { font-size: 1.1rem; font-weight: 700; color: var(--primary-dk); }
.snapshot-ts { font-size: .78rem; color: var(--text-2); }

.snapshot-stats {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: .75rem; margin-bottom: 1.25rem;
}
.snap-stat {
  text-align: center; padding: .75rem .5rem;
  background: var(--bg); border-radius: var(--radius);
}
.snap-num { display: block; font-size: 1.8rem; font-weight: 700; color: var(--primary); line-height: 1.2; }
.snap-label { font-size: .78rem; color: var(--text-2); font-weight: 500; text-transform: uppercase; letter-spacing: .04em; }

.snapshot-highlights {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: .75rem; margin-bottom: 1.25rem;
}
.snap-highlight {
  display: flex; align-items: center; gap: .75rem;
  padding: .75rem 1rem; border-radius: var(--radius); border-left: 4px solid var(--border);
  background: var(--bg);
}
.snap-hl-thrust  { border-left-color: #2563eb; }
.snap-hl-eff     { border-left-color: #16a34a; }
.snap-hl-current { border-left-color: #0891b2; }
.snap-hl-warn    { border-left-color: #dc2626; }
.snap-hl-icon { font-size: 1.5rem; flex-shrink: 0; }
.snap-hl-val  { font-size: 1.15rem; font-weight: 700; }
.snap-hl-desc { font-size: .8rem; color: var(--text-2); line-height: 1.3; }

.snapshot-section-title {
  font-size: .9rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: .04em; color: var(--text-2); margin-bottom: .5rem;
}
.snapshot-table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.snapshot-table th {
  background: var(--bg); text-align: left; padding: .45rem .6rem;
  font-weight: 600; border-bottom: 2px solid var(--border);
}
.snapshot-table td { padding: .45rem .6rem; border-bottom: 1px solid var(--border); }
.summary-sub { font-size: .75rem; color: var(--text-2); }

/* ── Drop zone ───────────────────────────────────────────────────── */
.drop-zone {
  border: 2.5px dashed var(--border); border-radius: 12px;
  padding: 3rem 2rem; text-align: center;
  cursor: pointer; transition: border-color .2s, background .2s;
  display: flex; flex-direction: column; align-items: center; gap: .6rem;
  background: var(--surface);
}
.drop-zone:hover, .drop-zone:focus-visible { border-color: var(--primary); background: var(--primary-lt); }
.drop-zone.drag-over { border-color: var(--primary); background: var(--primary-lt); }
.drop-zone svg { color: var(--text-2); }
.drop-text { font-size: 1.05rem; }
.drop-hint { font-size: .82rem; color: var(--text-2); }

.upload-actions { margin-top: 1rem; display: flex; gap: .75rem; flex-wrap: wrap; }

/* ── Saved Datasets Section ──────────────────────────────────────── */
.saved-datasets-section {
  margin-top: 2rem; padding-top: 1.5rem;
  border-top: 2px solid var(--border);
}
.saved-ds-title { font-size: 1.1rem; font-weight: 700; margin-bottom: .25rem; }
.saved-ds-hint  { font-size: .85rem; margin-bottom: 1rem; }

.ds-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1rem;
}
.ds-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1rem 1.15rem;
  box-shadow: var(--shadow); transition: border-color .15s, box-shadow .15s;
  display: flex; flex-direction: column; gap: .6rem;
}
.ds-card:hover { border-color: var(--primary); box-shadow: var(--shadow-lg); }

.ds-card-header { display: flex; justify-content: space-between; align-items: flex-start; }
.ds-card-name { font-size: 1rem; font-weight: 700; line-height: 1.3; color: var(--text); }
.ds-delete-btn {
  background: none; border: none; font-size: 1.3rem; line-height: 1;
  color: var(--text-2); cursor: pointer; padding: 0 .2rem;
  transition: color .15s;
}
.ds-delete-btn:hover { color: var(--red); }

.ds-card-meta { font-size: .78rem; color: var(--text-2); }

.ds-card-stats {
  display: flex; gap: 1rem; font-size: .85rem;
}
.ds-card-stats div { white-space: nowrap; }

.ds-card-detail { font-size: .8rem; color: var(--text-2); line-height: 1.5; }

.ds-load-btn { align-self: flex-start; margin-top: .25rem; }

/* ── Spinner ─────────────────────────────────────────────────────── */
.spinner { display: flex; align-items: center; gap: .75rem; margin: 1rem 0; color: var(--text-2); }
.spinner-ring {
  width: 24px; height: 24px; border: 3px solid var(--border);
  border-top-color: var(--primary); border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Toast ───────────────────────────────────────────────────────── */
.toast {
  position: fixed; top: 1rem; right: 1rem; z-index: 200;
  background: var(--text); color: #fff; padding: .6rem 1.2rem;
  border-radius: var(--radius); font-size: .88rem; box-shadow: var(--shadow-lg);
}

/* ── Warnings ────────────────────────────────────────────────────── */
.warn-box {
  background: var(--amber-lt); border: 1px solid var(--amber);
  border-radius: var(--radius); padding: .75rem 1rem; margin-top: 1rem; font-size: .88rem;
}
.warn-box summary { cursor: pointer; font-weight: 600; color: #92400e; }
.warn-box ul { margin-top: .5rem; padding-left: 1.25rem; }

/* ── File list table ─────────────────────────────────────────────── */
.file-table, .comp-table, .raw-table {
  width: 100%; border-collapse: collapse; font-size: .88rem; margin-top: .75rem;
}
.file-table th, .comp-table th, .raw-table th {
  background: var(--bg); text-align: left; padding: .5rem .65rem;
  font-weight: 600; border-bottom: 2px solid var(--border); white-space: nowrap;
}
.file-table td, .comp-table td, .raw-table td {
  padding: .45rem .65rem; border-bottom: 1px solid var(--border);
}
.comp-table tbody tr { cursor: pointer; transition: background .12s; }
.comp-table tbody tr:hover { background: var(--primary-lt); }
.sortable { cursor: pointer; user-select: none; }
.sortable:hover { color: var(--primary); }

.table-wrapper { overflow-x: auto; }
.table-actions { margin-top: .75rem; display: flex; gap: .5rem; }

.cell-best  { background: var(--green-lt); font-weight: 600; }
.cell-worst { background: var(--red-lt); }

/* ── KPI Cards ───────────────────────────────────────────────────── */
.kpi-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 1rem; margin-bottom: 1.5rem;
}
.kpi-card {
  background: var(--surface); border-radius: var(--radius); padding: 1rem 1.1rem;
  box-shadow: var(--shadow); border-left: 4px solid var(--primary);
}
.kpi-thrust     { border-left-color: #2563eb; }
.kpi-efficiency { border-left-color: #16a34a; }
.kpi-current    { border-left-color: #ea580c; }
.kpi-power      { border-left-color: #9333ea; }
.kpi-info       { border-left-color: #64748b; }
.kpi-title { font-size: .78rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--text-2); }
.kpi-value { font-size: 1.6rem; font-weight: 700; margin: .15rem 0; }
.kpi-sub   { font-size: .8rem; color: var(--text-2); }

/* ── Filter bar ──────────────────────────────────────────────────── */
.filter-bar { display: flex; gap: 1rem; flex-wrap: wrap; margin-top: .5rem; }
.filter-bar label { display: flex; align-items: center; gap: .35rem; font-size: .88rem; font-weight: 500; }
.filter-bar select {
  padding: .3rem .5rem; border: 1px solid var(--border); border-radius: var(--radius);
  font: inherit; font-size: .85rem; background: var(--surface);
}

/* ── Charts grid ─────────────────────────────────────────────────── */
.charts-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(480px, 1fr));
  gap: 1.25rem;
}
.chart-cell {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: .5rem; min-height: 460px;
}
.chart-cell-wide { grid-column: 1 / -1; }

.charts-filter-sticky {
  position: sticky; top: 49px; z-index: 40;
  background: var(--bg); padding: .5rem 0;
  border-bottom: 1px solid var(--border); margin-bottom: 1rem;
}

/* ── Settings button ─────────────────────────────────────────────── */
.settings-btn {
  background: none; border: none; font-size: 1.35rem; cursor: pointer;
  color: var(--text-2); padding: .25rem; margin-left: auto; line-height: 1;
}
.settings-btn:hover { color: var(--primary); }

/* ── Modal ────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,.4); display: flex; align-items: center; justify-content: center;
}
.modal-overlay[hidden] { display: none !important; }
.modal-card {
  background: var(--surface); border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.2);
  width: 90%; max-width: 460px; overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.25rem; border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 1.1rem; }
.modal-close {
  background: none; border: none; font-size: 1.5rem; cursor: pointer;
  color: var(--text-2); line-height: 1; padding: 0;
}
.modal-close:hover { color: var(--text); }
.modal-body { padding: 1.25rem; }
.settings-label { display: block; font-size: .9rem; font-weight: 500; color: var(--text); }
.settings-input {
  display: block; width: 100%; margin-top: .4rem; padding: .5rem .65rem;
  border: 1px solid var(--border); border-radius: var(--radius);
  font: inherit; font-size: .9rem;
}
.settings-input:focus { outline: none; border-color: var(--primary); }

/* ── AI Synopsis ──────────────────────────────────────────────────── */
.ai-synopsis-section {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 1.25rem; margin-bottom: 1.5rem;
}
.ai-synopsis-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: .75rem;
}
.ai-synopsis-header h3 { margin: 0; font-size: 1.05rem; }
.ai-synopsis-content p { margin: 0 0 .75rem 0; line-height: 1.65; color: var(--text); font-size: .92rem; }
.ai-synopsis-content p:last-child { margin-bottom: 0; }
.ai-synopsis-loading {
  display: flex; align-items: center; gap: .5rem; color: var(--text-2); font-size: .9rem;
}
.ai-synopsis-loading .spinner-dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--primary);
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: .3; } 50% { opacity: 1; } }
.ai-synopsis-error { color: #dc2626; font-size: .88rem; }

/* ── Detail view ─────────────────────────────────────────────────── */
#view-detail .section-header {
  position: sticky; top: 49px; z-index: 40;
  background: var(--bg); padding: .75rem 0 .75rem 0;
  border-bottom: 1px solid var(--border); margin-bottom: 1rem;
}
.detail-controls { display: flex; align-items: center; gap: 1rem; margin-top: .5rem; flex-wrap: wrap; }
.detail-controls select {
  padding: .35rem .6rem; border: 1px solid var(--border); border-radius: var(--radius);
  font: inherit; font-size: .9rem; max-width: 360px;
}
.detail-charts-grid {
  display: grid; grid-template-columns: repeat(2, 1fr);
  gap: 1.5rem; margin: 1rem 0;
}
.detail-chart-panel {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1rem; min-height: 400px;
}
@media (max-width: 960px) {
  .detail-charts-grid { grid-template-columns: 1fr; }
}

/* ── Thrust Lookup Card ──────────────────────────────────────────── */
.thrust-lookup-card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.25rem 1.5rem; margin: 1.25rem 0;
  border: 1px solid var(--border);
}
.thrust-lookup-card h3 { font-size: 1.05rem; margin-bottom: .25rem; }
.thrust-lookup-input {
  display: flex; align-items: center; gap: .75rem; flex-wrap: wrap;
  margin: .75rem 0;
}
.thrust-lookup-input label {
  display: flex; align-items: center; gap: .4rem; font-weight: 500; font-size: .9rem;
}
.thrust-lookup-input input {
  width: 130px; padding: .4rem .6rem; border: 1.5px solid var(--border);
  border-radius: var(--radius); font: inherit; font-size: .95rem;
  transition: border-color .15s;
}
.thrust-lookup-input input:focus { outline: none; border-color: var(--primary); }
.input-unit { font-size: .85rem; color: var(--text-2); }
.input-hint { font-size: .8rem; color: var(--text-2); margin-left: .25rem; }

.lookup-results-table { width: 100%; border-collapse: collapse; font-size: .85rem; margin-top: .75rem; }
.lookup-results-table th {
  background: var(--bg); text-align: left; padding: .5rem .6rem;
  font-weight: 600; border-bottom: 2px solid var(--border); white-space: nowrap;
}
.lookup-results-table td { padding: .45rem .6rem; border-bottom: 1px solid var(--border); }
.lookup-rank-1 { background: var(--green-lt); font-weight: 600; }
.lookup-out-of-range { color: var(--text-2); font-style: italic; }

/* ── Narrative ───────────────────────────────────────────────────── */
.narrative-box {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.25rem 1.5rem; margin-top: 1.25rem;
}
.narrative-heading { font-size: 1.05rem; font-weight: 700; margin-bottom: .5rem; }
.narrative-para   { margin-bottom: .6rem; line-height: 1.7; }
.narrative-bullet { margin-bottom: .3rem; padding-left: .5rem; }
.muted { color: var(--text-2); }
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 4px; font-size: .78rem; font-weight: 600; }
.badge-warn { background: var(--amber-lt); color: #92400e; }

/* ── Report ──────────────────────────────────────────────────────── */
.report-toolbar { margin-bottom: 1rem; }
.report-page {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 2rem 2.5rem;
}
.report-header { margin-bottom: 1.5rem; border-bottom: 2px solid var(--border); padding-bottom: 1rem; }
.report-header h2 { font-size: 1.5rem; }
.report-chart { min-height: 400px; margin-bottom: 1.5rem; }

/* ── Footer ──────────────────────────────────────────────────────── */
.app-footer {
  text-align: center; padding: 1rem; font-size: .8rem; color: var(--text-2);
  border-top: 1px solid var(--border); margin-top: 2rem;
}

/* ── Responsive ──────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .header-inner { padding: .5rem .75rem; }
  .app-main { padding: 1rem .75rem; }
  .charts-grid { grid-template-columns: 1fr; }
  .detail-charts-grid { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .report-page { padding: 1rem; }
}

/* ── Print styles ────────────────────────────────────────────────── */
@media print {
  .no-print, .app-header, .app-footer { display: none !important; }
  .view-section { display: none !important; }
  #view-report  { display: block !important; }
  .report-page  { box-shadow: none; padding: 0; }
  body { background: #fff; }
  .report-chart { break-inside: avoid; }
}

body.printing .view-section { display: none !important; }
body.printing #view-report  { display: block !important; }
