:root {
  --bg: #f3f4f7; --surface: #ffffff; --surface-2: #fafbfc; --border: #e4e7ec; --border-soft: #eef0f3;
  --text: #161a20; --text-soft: #5c6371; --text-faint: #98a0ac;
  --accent: #4338ca; --accent-2: #4f46e5; --accent-soft: #eef0fd; --accent-soft-2: #e0e2fb;
  --good: #059669; --good-soft: #e9f9f2; --good-border: #bfe9d6;
  --bad: #dc2626; --bad-soft: #fdedec; --bad-border: #f6c8c4;
  --warn: #c2760a; --warn-soft: #fdf3e2; --warn-border: #f2d8a3;
  --grey-tag: #f1f2f4; --grey-tag-text: #5c6371;
  --radius: 12px; --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 2px 8px rgba(16,24,40,.06);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

body {
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px; line-height: 1.45; min-width: 320px; min-height: 100vh;
}

.num { font-variant-numeric: tabular-nums; }
a { color: var(--accent); }
button { font-family: inherit; }

/* ---------------------------------------------------------------- buttons */
button {
  cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 9px;
  padding: 8px 14px; font-size: 13px; font-weight: 600; display: inline-flex; align-items: center; gap: 6px;
  transition: background .12s, border-color .12s;
}
button:hover { background: var(--surface-2); border-color: #d5d9e0; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
button.secondary { background: var(--surface); }
button:disabled { opacity: .5; cursor: not-allowed; }

/* ---------------------------------------------------------------- login page */
.center-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-card {
  width: 340px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 28px;
}
.login-card h1 { font-size: 18px; margin: 0 0 20px; }
.login-card label { display: block; margin: 14px 0 5px; font-size: 12.5px; color: var(--text-soft); font-weight: 600; }
.login-card input { width: 100%; }
.login-card button { width: 100%; justify-content: center; margin-top: 18px; }
.error { color: var(--bad); font-size: 13px; min-height: 1.2em; margin-top: 10px; }

input[type="email"], input[type="password"], input[type="number"], input[type="file"], input[type="search"] {
  border: 1px solid var(--border); border-radius: 9px; padding: 8px 12px; font-size: 13px; font-family: inherit;
  background: #fff; color: var(--text);
}
input[type="number"] { width: 100px; text-align: right; }

label { display: block; font-size: 12px; color: var(--text-soft); font-weight: 600; margin-bottom: 5px; }

/* ---------------------------------------------------------------- topbar + tabs */
.topbar { position: sticky; top: 0; z-index: 50; background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner { max-width: 1360px; margin: 0 auto; padding: 14px 24px; display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.logo { display: flex; align-items: center; gap: 10px; }
.logo-mark {
  width: 34px; height: 34px; border-radius: 9px; background: linear-gradient(135deg, var(--accent), #7c74f0);
  display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 800; font-size: 15px; flex: none;
}
.logo-text { display: flex; flex-direction: column; line-height: 1.1; }
.logo-text b { font-size: 15px; font-weight: 800; }
.logo-text span { font-size: 11.5px; color: var(--text-faint); }
.topbar-spacer { flex: 1; }
.who-pill { font-size: 12.5px; color: var(--text-soft); background: var(--surface-2); border: 1px solid var(--border); padding: 5px 10px; border-radius: 999px; white-space: nowrap; }
.who-pill b { color: var(--text); }

.tabs { position: sticky; top: 63px; z-index: 40; background: var(--bg); border-bottom: 1px solid var(--border); }
.tabs-inner { max-width: 1360px; margin: 0 auto; padding: 0 24px; display: flex; gap: 4px; overflow-x: auto; }
.tab-btn {
  background: transparent; border: none; border-radius: 0;
  padding: 12px 14px; font-size: 13.5px; font-weight: 600; color: var(--text-soft); cursor: pointer;
  border-bottom: 2.5px solid transparent; white-space: nowrap;
}
.tab-btn:hover { color: var(--text); background: transparent; border-color: transparent; }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }

.wrap { max-width: 1360px; margin: 0 auto; padding: 22px 24px 60px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ---------------------------------------------------------------- cards / sections */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 4px; font-size: 14px; font-weight: 700; }
.card .sub { font-size: 12px; color: var(--text-faint); margin-bottom: 12px; }
.section-title { font-size: 16px; font-weight: 800; margin: 28px 0 12px; }
.section-title:first-child { margin-top: 0; }

/* ---------------------------------------------------------------- kpi */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; }
.kpi-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.kpi-label { font-size: 12px; color: var(--text-soft); font-weight: 600; }
.kpi-value { font-size: 22px; font-weight: 800; margin-top: 6px; font-variant-numeric: tabular-nums; }
.kpi-value.pos { color: var(--good); }
.kpi-value.neg { color: var(--bad); }

/* ---------------------------------------------------------------- category / generic bar chart */
.bar-row { display: flex; align-items: center; gap: 10px; margin: 10px 0; }
.bar-row .bar-label { width: 160px; flex: none; font-size: 12.5px; color: var(--text-soft); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { flex: 1; background: var(--border-soft); border-radius: 6px; height: 22px; overflow: hidden; }
.bar-fill { height: 100%; background: linear-gradient(90deg, var(--accent-2), #8b85f2); border-radius: 6px; }
.bar-fill.neg { background: linear-gradient(90deg, var(--bad), #f28b83); }
.bar-row .bar-value { width: 150px; flex: none; text-align: right; font-size: 12.5px; color: var(--text-faint); font-weight: 600; }

/* ---------------------------------------------------------------- tables */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em; color: var(--text-faint);
  font-weight: 700; padding: 9px 10px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text-soft); }
td { padding: 9px 10px; border-bottom: 1px solid var(--border-soft); vertical-align: middle; }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- badges */
.badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 700; }
.badge-red { background: var(--bad-soft); color: var(--bad); border: 1px solid var(--bad-border); }
.badge-orange { background: #fdf1e4; color: #b4530a; border: 1px solid #f2d0a3; }
.badge-yellow { background: var(--warn-soft); color: var(--warn); border: 1px solid var(--warn-border); }
.badge-grey { background: var(--grey-tag); color: var(--grey-tag-text); border: 1px solid var(--border); }
.badge-green { background: var(--good-soft); color: var(--good); border: 1px solid var(--good-border); }

/* ---------------------------------------------------------------- insight boxes */
.insight {
  display: flex; gap: 10px; padding: 11px 12px; border-radius: 9px; margin-bottom: 8px; font-size: 13px; align-items: flex-start;
  border: 1px solid var(--border-soft); background: var(--surface-2);
}
.insight .ic { flex: none; font-size: 15px; line-height: 1.3; }
.insight.warn { background: var(--warn-soft); border-color: var(--warn-border); }
.insight.bad { background: var(--bad-soft); border-color: var(--bad-border); }
.insight.good { background: var(--good-soft); border-color: var(--good-border); }

/* ---------------------------------------------------------------- expand row / waterfall */
tr.expand-row td { background: var(--surface-2); padding: 16px 18px 20px; }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
@media (max-width: 800px) { .detail-grid { grid-template-columns: 1fr; } }
.mini-flow { display: flex; align-items: stretch; gap: 8px; flex-wrap: wrap; }
.flow-step { background: var(--surface); border: 1px solid var(--border); border-radius: 9px; padding: 9px 12px; min-width: 108px; }
.flow-step .l { font-size: 10.5px; color: var(--text-faint); font-weight: 700; text-transform: uppercase; letter-spacing: .03em; }
.flow-step .v { font-size: 14.5px; font-weight: 800; margin-top: 3px; }
.flow-step.neg .v { color: var(--bad); }
.flow-step.pos .v { color: var(--good); }

/* ---------------------------------------------------------------- funnel */
.funnel-row { display: flex; align-items: center; gap: 10px; margin: 10px 0; }
.funnel-label { width: 150px; font-size: 12.5px; color: var(--text-soft); font-weight: 600; flex: none; }
.funnel-bar-wrap { flex: 1; background: var(--border-soft); border-radius: 6px; height: 30px; overflow: hidden; }
.funnel-bar { height: 100%; background: linear-gradient(90deg, var(--accent-2), #8b85f2); border-radius: 6px; display: flex; align-items: center; padding: 0 10px; color: #fff; font-size: 12.5px; font-weight: 700; white-space: nowrap; }
.funnel-pct { width: 90px; text-align: right; font-size: 12px; color: var(--text-faint); font-weight: 600; flex: none; }

/* ---------------------------------------------------------------- problem cards */
.problem-card { border: 1px solid var(--border); border-radius: 10px; padding: 13px 14px; background: var(--surface); margin-bottom: 10px; }
.problem-card .ph { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 4px; }
.problem-card .pname { font-weight: 700; font-size: 13.5px; }
.problem-card .pmeta { font-size: 12px; color: var(--text-soft); }

/* ---------------------------------------------------------------- stock */
.stock-status { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 700; padding: 3px 9px; border-radius: 999px; }

/* ---------------------------------------------------------------- plan progress */
.progress-wrap { background: var(--border-soft); border-radius: 8px; height: 16px; position: relative; overflow: hidden; margin-top: 8px; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); border-radius: 8px; }
.progress-fill.behind { background: linear-gradient(90deg, var(--warn), #e0a63c); }
.progress-fill.over { background: linear-gradient(90deg, var(--bad), #ef6a63); }
.progress-marker { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--text); }
.plan-row { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 2px; }
.plan-row .muted { color: var(--text-faint); }
.plan-form { display: flex; gap: 14px; align-items: end; flex-wrap: wrap; }
.plan-form > div { display: flex; flex-direction: column; gap: 5px; }
.plan-form label { font-size: 12px; color: var(--text-soft); font-weight: 600; }

/* ---------------------------------------------------------------- toolbar (search/filter/sort) */
.search-row { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; align-items: center; }
.search-row input[type="search"] { flex: 1; min-width: 200px; }
.select, select { border: 1px solid var(--border); border-radius: 9px; padding: 8px 12px; font-size: 13px; font-family: inherit; background: #fff; color: var(--text); }

/* ---------------------------------------------------------------- misc */
/* ---------------------------------------------------------------- upload modal */
.overlay {
  position: fixed; inset: 0; background: rgba(15,17,22,.45);
  display: none; align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.overlay.show { display: flex; }
.modal { background: var(--surface); border-radius: 14px; width: 100%; max-width: 520px; padding: 22px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.modal h2 { margin: 0 0 4px; font-size: 16px; }
.modal .sub { margin-bottom: 0; }
.modal-close { background: transparent; border: none; padding: 4px 8px; font-size: 18px; color: var(--text-faint); }
.modal-close:hover { color: var(--text); background: var(--surface-2); }
.report-types { list-style: none; padding: 0; margin: 14px 0; font-size: 12.5px; color: var(--text-soft); }
.report-types li { padding: 5px 0 5px 20px; position: relative; border-bottom: 1px solid var(--border-soft); }
.report-types li:last-child { border-bottom: none; }
.report-types li::before { content: "•"; position: absolute; left: 4px; color: var(--accent); }
.report-types b { color: var(--text); }
.modal-actions { display: flex; justify-content: flex-end; margin-top: 16px; }

.message { margin-top: 12px; font-size: 13px; color: var(--good); min-height: 1.2em; }
.message.error { color: var(--bad); }
.empty-hint { border: 1px dashed var(--border); border-radius: 10px; padding: 14px; font-size: 12.5px; color: var(--text-soft); background: var(--surface-2); }
.footnote { font-size: 11.5px; color: var(--text-faint); margin-top: 6px; }
a.export-link { font-size: 13px; font-weight: 600; text-decoration: none; }
a.export-link:hover { text-decoration: underline; }
