:root {
  --bg: #f6f7f9;
  --ink: #172026;
  --muted: #60717c;
  --line: #d8dee4;
  --panel: #ffffff;
  --accent: #0f766e;
  --warn: #a15c00;
  --bad: #b42318;
  --good: #167145;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  color: var(--ink);
  background: var(--bg);
  font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(135deg, rgba(15, 118, 110, 0.08), rgba(23, 32, 38, 0.02)),
    #eef2f4;
}
.login-card {
  width: min(100%, 380px);
  padding: 28px;
  border: 1px solid rgba(23, 32, 38, 0.12);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 20px 60px rgba(23, 32, 38, 0.12);
}
.login-brand {
  margin-bottom: 22px;
}
.login-brand span {
  display: inline-block;
  margin-bottom: 8px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
}
.login-brand h1 {
  font-size: 24px;
}
.login-card label {
  display: grid;
  gap: 7px;
  margin-top: 14px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}
.login-card input,
.login-card button {
  width: 100%;
  height: 42px;
}
.login-card button {
  margin-top: 18px;
}
.login-status {
  min-height: 20px;
  margin-top: 12px;
  color: var(--muted);
}
.app-shell { min-height: 100vh; }
.auth-pending .app-shell { display: none; }
.is-authenticated .login-screen { display: none; }

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  align-items: center;
  padding: 18px 24px;
  border-bottom: 1px solid var(--line);
  background: #fff;
}
h1 { margin: 0; font-size: 22px; }
h2 { margin: 0 0 12px; font-size: 15px; }
h3 { margin: 0 0 6px; font-size: 14px; }
p { margin: 4px 0 0; color: var(--muted); }

.userbar, .toolbar {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.userbar span {
  color: var(--muted);
  font-size: 13px;
}
input, select, button {
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
  color: var(--ink);
}
button {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 600;
}
button.secondary { background: #fff; color: var(--ink); border-color: var(--line); }

.tabs {
  display: flex;
  gap: 2px;
  padding: 8px 24px 0;
  background: #fff;
  border-bottom: 1px solid var(--line);
}
.tabs button {
  background: #fff;
  color: var(--muted);
  border: 0;
  border-bottom: 3px solid transparent;
  border-radius: 0;
}
.tabs button.active {
  color: var(--ink);
  border-bottom-color: var(--accent);
}

main { padding: 18px 24px 28px; }
.view { display: none; }
.view.active { display: block; }
.toolbar { margin-bottom: 16px; }

.metrics {
  display: grid;
  grid-template-columns: repeat(6, minmax(120px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}
.metric {
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.metric span { display: block; color: var(--muted); font-size: 12px; }
.metric strong { display: block; margin-top: 4px; font-size: 20px; }

.grid { display: grid; gap: 14px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.panel {
  min-width: 0;
  padding: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.panel.wide { grid-column: 1 / -1; }
.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.panel-title button {
  height: 28px;
  flex: 0 0 auto;
  padding: 0 8px;
  font-size: 12px;
}
.analysis-compact {
  grid-template-columns: minmax(220px, 0.9fr) minmax(280px, 1.2fr) minmax(280px, 1.2fr);
  align-items: start;
}
.compact-panel {
  position: relative;
  overflow: hidden;
}
.compact-panel.is-compact .compact-body {
  max-height: 260px;
  overflow: hidden;
}
.compact-panel.is-compact::after {
  content: "";
  position: absolute;
  left: 1px;
  right: 1px;
  bottom: 1px;
  height: 42px;
  pointer-events: none;
  background: linear-gradient(rgba(255, 255, 255, 0), var(--panel));
  border-radius: 0 0 8px 8px;
}
.analysis-compact.has-expanded {
  grid-template-columns: 1fr;
}
.analysis-compact.has-expanded .compact-panel:not(.is-expanded) {
  display: none;
}
.compact-panel.is-expanded {
  grid-column: 1 / -1;
  width: 100%;
  z-index: 2;
}
.compact-panel.is-expanded .compact-body {
  max-height: none;
  overflow: visible;
}
#stockAnalysis .metrics,
#stockProductAnalysis .metrics,
#analysis .metrics {
  grid-template-columns: repeat(10, minmax(86px, 1fr));
  gap: 8px;
}
#stockAnalysis .metric,
#stockProductAnalysis .metric,
#analysis .metric {
  padding: 9px 10px;
}
#stockAnalysis .metric strong,
#stockProductAnalysis .metric strong,
#analysis .metric strong {
  font-size: 16px;
}
#stockAnalysis th,
#stockAnalysis td,
#stockProductAnalysis th,
#stockProductAnalysis td,
#analysis th,
#analysis td {
  padding: 6px 5px;
  font-size: 12px;
}
.note {
  margin: 8px 0 16px;
  color: var(--warn);
}

table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}
th, td {
  padding: 7px 6px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  overflow-wrap: anywhere;
}
th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}
.pill {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid var(--line);
}
.pill.good { color: var(--good); border-color: #9bd3b6; background: #effaf4; }
.pill.warn { color: var(--warn); border-color: #e8c17a; background: #fff8e8; }
.pill.bad { color: var(--bad); border-color: #f1b7b0; background: #fff3f1; }
.muted { color: var(--muted); }
a { color: var(--accent); text-decoration: none; }
canvas { width: 100%; max-width: 100%; height: 300px; border: 1px solid var(--line); border-radius: 6px; }

.insight {
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.insight:first-child { padding-top: 0; }
.insight:last-child { border-bottom: 0; }
.barrow {
  display: grid;
  grid-template-columns: minmax(92px, 160px) 1fr 48px;
  gap: 8px;
  align-items: center;
  margin-top: 8px;
}
.barrow span, .barrow strong {
  color: var(--muted);
  font-size: 12px;
}
.bar {
  height: 8px;
  background: #eef1f3;
  border-radius: 999px;
  overflow: hidden;
}
.bar i {
  display: block;
  height: 100%;
  background: var(--accent);
}

@media (max-width: 900px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .userbar { width: 100%; justify-content: space-between; }
  #stockAnalysis .metrics,
  #stockProductAnalysis .metrics,
  #analysis .metrics,
  .metrics { grid-template-columns: repeat(2, minmax(120px, 1fr)); }
  .grid.two, .grid.three, .analysis-compact { grid-template-columns: 1fr; }
  .compact-panel.is-compact .compact-body { max-height: 220px; }
}
