:root {
  color-scheme: light;
  --page: #f3f5f8;
  --surface: #ffffff;
  --surface-muted: #f8fafc;
  --line: #dce2ea;
  --line-strong: #c7d0dd;
  --text: #172033;
  --muted: #687386;
  --blue: #2367f2;
  --blue-dark: #174ec1;
  --blue-soft: #eaf1ff;
  --green: #138a5b;
  --green-soft: #e8f7f0;
  --orange: #b96713;
  --orange-soft: #fff1df;
  --red: #c83745;
  --red-soft: #fff0f1;
  --shadow: 0 18px 55px rgba(31, 45, 76, 0.08);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  background: var(--page);
  color: var(--text);
}

button, input, select, textarea { font: inherit; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  min-height: 78px;
  padding: 14px clamp(20px, 4vw, 64px);
  background: rgba(255, 255, 255, 0.94);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(18px);
}

.topbar-title, .topbar-actions, .intro-row, .result-heading {
  display: flex;
  align-items: center;
}

.topbar-title { gap: 13px; }
.topbar-actions { gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.topbar h1, .intro-row h2, .result-heading h2 {
  margin: 0;
  letter-spacing: -0.025em;
}

.topbar h1 { font-size: 19px; }

.eyebrow {
  margin: 0 0 3px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 750;
  letter-spacing: 0.14em;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: 15px;
  background: linear-gradient(145deg, #2d75ff, #174cc8);
  color: white;
  font-weight: 800;
  letter-spacing: -0.05em;
  box-shadow: 0 10px 24px rgba(35, 103, 242, 0.26);
}

.brand-mark.small { width: 44px; height: 44px; border-radius: 13px; }

.user-caption { color: var(--muted); font-size: 11px; }

.top-kpis {
  display: grid;
  flex: 1 1 620px;
  grid-template-columns: repeat(4, minmax(118px, 1fr));
  gap: 8px;
  max-width: 720px;
}

.top-kpi {
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-muted);
}

.top-kpi span { display: block; color: var(--muted); font-size: 9px; font-weight: 750; letter-spacing: 0.06em; }
.top-kpi strong { display: block; margin-top: 3px; overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.top-kpi.generation-card { border-color: #a9dfc6; background: var(--green-soft); }
.top-kpi.profit-card { border-color: #b8cffd; background: var(--blue-soft); }

.page-shell {
  width: min(100% - 40px, 1680px);
  margin: 0 auto;
  padding: 38px 0 60px;
}

.workspace-toolbar,
.scenario-panel,
.collapsible-section,
.admin-card {
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.workspace-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 19px 21px;
}

.workspace-copy h2, .scenario-heading h2, .admin-card h2 { margin: 0; font-size: 20px; }
.workspace-copy p, .scenario-heading p, .admin-card p { margin: 5px 0 0; color: var(--muted); font-size: 13px; }
.workspace-actions, .scenario-controls { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.workspace-actions { justify-content: flex-end; }

.scenario-panel { margin-top: 16px; padding: 18px 20px; }
.scenario-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; }
.scenario-controls { margin-top: 15px; }
.scenario-controls select { flex: 1 1 360px; min-width: 220px; }
.scenario-details { margin-top: 12px; padding: 11px 13px; border-radius: 10px; background: var(--surface-muted); color: var(--muted); font-size: 13px; line-height: 1.45; }

.intro-row, .result-heading {
  justify-content: space-between;
  gap: 24px;
}

.intro-row p, .result-heading p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.intro-row h2, .result-heading h2 { font-size: 25px; }

.upload-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  margin-top: 20px;
}

.upload-card {
  position: relative;
  display: flex;
  align-items: center;
  min-height: 112px;
  gap: 16px;
  padding: 22px;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: border-color 160ms ease, transform 160ms ease, background 160ms ease;
}

.upload-card:hover, .upload-card.dragging {
  transform: translateY(-2px);
  border-color: #8eb2ff;
  background: #fbfdff;
}

.upload-card.uploading { opacity: 0.65; pointer-events: none; }
.upload-card input { position: absolute; opacity: 0; pointer-events: none; }

.file-icon {
  display: grid;
  flex: 0 0 auto;
  place-items: center;
  width: 52px;
  height: 60px;
  border-radius: 13px;
  background: var(--blue-soft);
  color: var(--blue);
  font-size: 20px;
  font-weight: 800;
}

.file-icon.alternate { background: #efeafe; color: #7253d6; }

.upload-copy { display: flex; min-width: 0; flex: 1; flex-direction: column; gap: 7px; }
.upload-copy strong { font-size: 17px; }
.upload-copy span { overflow: hidden; color: var(--muted); font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.replace-label { color: var(--blue); font-size: 13px; font-weight: 700; }

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 0 13px;
  border-radius: 999px;
  background: #eef1f5;
  color: #5d6879;
  font-size: 13px;
  font-weight: 650;
}

.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #8b96a6; }
.status-pill.ready { color: var(--green); background: var(--green-soft); }
.status-pill.ready .status-dot { background: var(--green); }
.status-pill.working { color: var(--blue); background: var(--blue-soft); }
.status-pill.working .status-dot { background: var(--blue); animation: pulse 1.1s infinite; }
.status-pill.error { color: var(--red); background: var(--red-soft); }
.status-pill.error .status-dot { background: var(--red); }
.status-pill.dirty { color: #94620c; background: #fff3d3; }
.status-pill.dirty .status-dot { background: #d79720; }

@keyframes pulse { 50% { opacity: 0.25; transform: scale(0.75); } }

.ghost-button, .primary-button, .download-button, .calculate-button,
.input-download-button, .secondary-button, .danger-outline-button,
.collapse-button, .table-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  border-radius: 11px;
  border: 0;
  text-decoration: none;
  cursor: pointer;
}

.ghost-button { padding: 0 14px; color: var(--text); background: #eef1f5; }
.calculate-button { padding: 0 16px; color: #fff; background: var(--blue); font-weight: 750; }
.calculate-button:hover:not(:disabled) { background: var(--blue-dark); }
.calculate-button:disabled { cursor: wait; opacity: 0.65; }
.download-button { min-height: 46px; padding: 0 19px; color: #fff; background: var(--blue); font-size: 14px; font-weight: 750; }
.download-button:hover { background: var(--blue-dark); }
.download-button.disabled { color: #929dad; background: #e2e6ec; pointer-events: none; }
.download-button:disabled { cursor: wait; opacity: 0.68; }
.secondary-button, .collapse-button { min-height: 38px; padding: 0 13px; border: 1px solid var(--line-strong); color: var(--text); background: #fff; font-weight: 700; }
.secondary-button:hover, .collapse-button:hover { border-color: var(--blue); color: var(--blue); }
.secondary-button:disabled, .danger-outline-button:disabled { cursor: default; opacity: 0.45; }
.danger-outline-button { min-height: 38px; padding: 0 13px; border: 1px solid #efb8be; color: var(--red); background: #fff; font-weight: 700; }
.danger-outline-button:hover:not(:disabled) { background: var(--red-soft); }
.button-link { text-decoration: none; }
select, .portal-dialog input, .portal-dialog textarea, .admin-create-form input, .admin-create-form select {
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  outline: none;
  background: #fff;
  color: var(--text);
}
select:focus, .portal-dialog input:focus, .portal-dialog textarea:focus, .admin-create-form input:focus, .admin-create-form select:focus { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-soft); }

.message {
  margin-top: 16px;
  padding: 13px 16px;
  border: 1px solid #b8cffd;
  border-radius: 12px;
  background: var(--blue-soft);
  color: #1c54bd;
  font-size: 14px;
}

.message.success { border-color: #a7dfc8; background: var(--green-soft); color: var(--green); }
.message.error { border-color: #f0bcc2; background: var(--red-soft); color: var(--red); }

.editor-section { margin-top: 16px; }
.editor-heading { display: flex; align-items: center; justify-content: space-between; gap: 20px; }
.editor-heading h2 { margin: 0; font-size: 25px; letter-spacing: -0.025em; }
.editor-heading p { margin: 6px 0 0; color: var(--muted); font-size: 14px; }
.editor-actions { display: flex; flex: 0 0 auto; align-items: center; gap: 9px; }
.change-badge { flex: 0 0 auto; padding: 8px 11px; border-radius: 999px; background: #fff6dc; color: #8a6512; font-size: 12px; font-weight: 750; }
.input-download-button { min-height: 36px; padding: 0 12px; border: 1px solid #b8cffd; color: var(--blue-dark); background: var(--blue-soft); font-size: 12px; font-weight: 750; }
.input-download-button:hover { border-color: var(--blue); }

.sheet-tabs { display: flex; gap: 7px; padding: 14px 16px 9px; overflow-x: auto; scrollbar-width: thin; }
.sheet-tab { flex: 0 0 auto; height: 36px; padding: 0 13px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: #586477; cursor: pointer; font-size: 12px; font-weight: 700; }
.sheet-tab:hover { border-color: #9ab8f3; color: var(--blue-dark); }
.sheet-tab.active { border-color: var(--blue); background: var(--blue); color: white; box-shadow: 0 7px 17px rgba(35, 103, 242, 0.2); }

.editor-panel { overflow: hidden; border-top: 1px solid var(--line); background: var(--surface); }
.editor-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 18px; min-height: 58px; padding: 10px 15px; border-bottom: 1px solid var(--line); background: var(--surface-muted); }
.editor-toolbar > div:first-child { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.editor-toolbar strong { font-size: 15px; }
.editor-toolbar span { color: var(--muted); font-size: 12px; }
.pagination { display: flex; align-items: center; gap: 9px; }
.page-button { width: 34px; height: 34px; border: 1px solid var(--line); border-radius: 9px; background: white; color: var(--text); cursor: pointer; }
.page-button:hover:not(:disabled) { border-color: var(--blue); color: var(--blue); }
.page-button:disabled { opacity: 0.4; cursor: default; }
.editor-loading { display: grid; min-height: 180px; place-items: center; color: var(--muted); font-size: 13px; }
.workbook-table-scroll { max-height: 480px; overflow: auto; }

.workbook-table th, .workbook-table td { height: 34px; min-width: 0; max-width: 230px; padding: 6px 9px; overflow: hidden; text-align: left; text-overflow: ellipsis; }
.workbook-table thead th { background: #edf2f8; text-align: left; }
.workbook-table th:first-child, .workbook-table td:first-child { position: sticky; left: 0; z-index: 3; min-width: 52px; width: 52px; max-width: 52px; background: #f1f4f8; color: #7a8596; text-align: center; }
.workbook-table th:nth-child(2), .workbook-table td:nth-child(2) { position: sticky; left: 52px; z-index: 3; min-width: 0; max-width: 230px; }
.workbook-table td[contenteditable="true"] { cursor: text; outline: none; }
.workbook-table td[contenteditable="true"]:focus { position: relative; z-index: 6; background: #fff; box-shadow: inset 0 0 0 2px var(--blue); }
.workbook-table td.changed-cell { background: #fff7d9; box-shadow: inset 3px 0 0 #e6aa22; }
.workbook-table td.saving-cell { background: var(--blue-soft); opacity: 0.7; }
.filter-row input, .summary-filter-row input { width: 100%; min-width: 82px; height: 28px; padding: 0 8px; border: 1px solid #cdd6e2; border-radius: 7px; outline: none; background: #fff; color: var(--text); font-size: 11px; font-weight: 500; }
.filter-row input:focus, .summary-filter-row input:focus { border-color: var(--blue); box-shadow: 0 0 0 2px rgba(35, 103, 242, 0.12); }
.workbook-table thead tr:first-child th { top: 0; }
.workbook-table thead .filter-row th { top: 42px; height: 38px; padding: 5px; background: #f7f9fc; }

.result-section { margin-top: 16px; }
.result-heading { margin-top: 0; align-items: center; }
.summary-cards { display: flex; gap: 10px; }
.summary-card { min-width: 170px; padding: 13px 15px; border: 1px solid var(--line); border-radius: 13px; background: var(--surface); }
.summary-card span { display: block; margin-bottom: 5px; color: var(--muted); font-size: 11px; }
.summary-card strong { font-size: 15px; }
.profit-card strong { font-size: 18px; color: var(--blue-dark); }
.generation-card { border-color: #a9dfc6; background: var(--green-soft); }
.generation-card strong { font-size: 18px; color: var(--green); }
.check-good { color: var(--green); }
.check-bad { color: var(--red); }

.table-panel {
  min-height: 330px;
  margin-top: 0;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  border: 0;
  border-radius: 0 0 18px 18px;
  box-shadow: none;
}

.empty-state { display: grid; place-items: center; align-content: center; min-height: 330px; gap: 8px; color: var(--muted); }
.empty-state strong { color: var(--text); }
.empty-state span { font-size: 13px; }
.spinner { width: 30px; height: 30px; margin-bottom: 6px; border: 3px solid #d8e4fb; border-top-color: var(--blue); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.table-scroll { max-height: 68vh; overflow: auto; }
table { width: max-content; min-width: 100%; border-collapse: separate; border-spacing: 0; font-variant-numeric: tabular-nums; table-layout: fixed; }
th, td { height: 36px; padding: 7px 11px; border-right: 1px solid #e7ebf0; border-bottom: 1px solid #e7ebf0; background: #fff; text-align: right; white-space: nowrap; font-size: 12px; }
thead th { position: sticky; top: 0; z-index: 5; height: 42px; background: #edf2f8; color: #425066; font-weight: 750; }
#summary-table thead tr:first-child th { top: 0; }
#summary-table thead .summary-filter-row th { top: 42px; height: 38px; padding: 5px; background: #f7f9fc; }
#summary-table th:first-child, #summary-table td:first-child { position: sticky; left: 0; z-index: 3; text-align: left; }
#summary-table th:nth-child(2), #summary-table td:nth-child(2) { position: sticky; left: var(--summary-first-width, 82px); z-index: 3; max-width: 230px; text-align: left; }
thead th:first-child, thead th:nth-child(2) { z-index: 7; background: #e7edf5; }
tbody th:first-child, tbody td:nth-child(2) { background: #fafbfd; }
.month-start-row > * { border-top: 2px solid var(--line-strong); }
.month-start-row th:first-child { color: var(--blue-dark); font-weight: 800; }
.grand-total-row > * { background: #edf4ff !important; font-weight: 750; }
.grand-total-row:first-of-type > * { border-top: 3px solid #7aa5f8; }
.metric-w-row > * { background: #e4f6eb !important; color: #176844; font-weight: 750; }
.metric-s-row > * { background: var(--orange-soft) !important; color: #8b4d0e; font-weight: 750; }
.metric-w-row > * { border-bottom-color: #b9e1c9; }
.metric-s-row > * { border-bottom-color: #efc99e; }
.negative-value { color: var(--red); background: #fff6f7; }
.filtered-out { display: none; }

.collapsible-section { overflow: visible; }
.collapsible-heading { min-height: 72px; padding: 12px 16px; }
.section-toggle { display: flex; align-items: center; min-width: 0; gap: 11px; padding: 0; border: 0; background: transparent; color: var(--text); text-align: left; cursor: pointer; }
.section-toggle > span:last-child { display: flex; min-width: 0; flex-direction: column; gap: 4px; }
.section-toggle strong { font-size: 20px; letter-spacing: -0.025em; }
.section-toggle small { overflow: hidden; color: var(--muted); font-size: 12px; font-weight: 500; text-overflow: ellipsis; white-space: nowrap; }
.chevron { color: var(--blue); font-size: 30px; line-height: 1; transition: transform 160ms ease; }
.section-toggle[aria-expanded="true"] .chevron { transform: rotate(90deg); }
.collapsible-section.collapsed { box-shadow: 0 8px 24px rgba(31, 45, 76, 0.05); }
.collapsible-body { border-top: 1px solid var(--line); }

.portal-dialog { width: min(92vw, 540px); padding: 0; border: 0; border-radius: 18px; color: var(--text); box-shadow: 0 30px 100px rgba(22, 33, 57, 0.28); }
.portal-dialog::backdrop { background: rgba(20, 28, 44, 0.52); backdrop-filter: blur(3px); }
.portal-dialog form { display: grid; gap: 15px; padding: 22px; }
.portal-dialog label { display: grid; gap: 7px; color: #4e5a6e; font-size: 13px; font-weight: 700; }
.portal-dialog .checkbox-label { display: flex; align-items: center; gap: 9px; font-weight: 600; }
.portal-dialog .checkbox-label input { min-height: 0; width: 17px; height: 17px; padding: 0; }
.portal-dialog textarea { min-height: 110px; padding-top: 10px; resize: vertical; }
.dialog-heading, .dialog-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.dialog-heading h2 { margin: 0; font-size: 22px; }
.dialog-close { width: 34px; height: 34px; border: 0; border-radius: 9px; background: #eef1f5; color: var(--text); cursor: pointer; font-size: 22px; }
.dialog-actions { justify-content: flex-end; margin-top: 4px; }

.admin-shell { display: grid; gap: 16px; }
.admin-card { padding: 20px; }
.admin-create-form { display: grid; grid-template-columns: 1fr 1.2fr 1fr 180px auto; gap: 12px; align-items: end; margin-top: 18px; }
.admin-create-form label { display: grid; gap: 6px; color: #4e5a6e; font-size: 12px; font-weight: 700; }
.admin-table-scroll { margin-top: 16px; overflow: auto; }
.admin-table { min-width: 900px; }
.admin-table th, .admin-table td { position: static !important; min-width: 0 !important; text-align: left; }
.admin-actions { display: flex; gap: 6px; }
.table-action { min-height: 30px; padding: 0 9px; border: 1px solid var(--line); background: #fff; color: var(--text); font-size: 11px; font-weight: 700; }
.table-action.danger { border-color: #efb8be; color: var(--red); }

.hidden { display: none !important; }

.login-page { display: grid; min-height: 100vh; place-items: center; padding: 24px; background: radial-gradient(circle at 20% 10%, #e6efff, transparent 35%), var(--page); }
.login-card { width: min(100%, 410px); padding: 38px; border: 1px solid var(--line); border-radius: 24px; background: rgba(255, 255, 255, 0.96); box-shadow: 0 28px 85px rgba(31, 45, 76, 0.14); }
.login-card .brand-mark { margin-bottom: 26px; }
.login-card h1 { margin: 0; font-size: 31px; letter-spacing: -0.04em; }
.login-copy { margin: 10px 0 25px; color: var(--muted); line-height: 1.5; }
.login-form { display: grid; gap: 10px; }
.login-form label { color: #4e5a6e; font-size: 13px; font-weight: 700; }
.login-form input { width: 100%; height: 48px; padding: 0 14px; border: 1px solid var(--line-strong); border-radius: 11px; outline: none; }
.login-form input:focus { border-color: var(--blue); box-shadow: 0 0 0 4px var(--blue-soft); }
.primary-button { margin-top: 7px; min-height: 48px; color: white; background: var(--blue); font-weight: 750; }
.primary-button:hover { background: var(--blue-dark); }
.form-error { margin: 0; color: var(--red); font-size: 13px; }

@media (max-width: 1000px) {
  .result-heading { align-items: flex-start; flex-direction: column; }
  .summary-cards { width: 100%; overflow-x: auto; }
  .summary-card { flex: 1 0 170px; }
}

@media (max-width: 720px) {
  .topbar { position: static; align-items: stretch; flex-direction: column; gap: 10px; padding: 12px; }
  .topbar-actions { align-items: center; justify-content: flex-start; flex-direction: row; }
  .status-pill { max-width: 190px; }
  .top-kpis { width: 100%; max-width: none; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .top-kpi { padding: 7px 9px; }
  .page-shell { width: min(100% - 24px, 1680px); padding-top: 25px; }
  .intro-row { align-items: flex-start; flex-direction: column; }
  .workspace-toolbar, .scenario-heading { align-items: stretch; flex-direction: column; }
  .workspace-actions { justify-content: flex-start; }
  .workspace-actions > *, .scenario-controls > * { flex: 1 1 auto; }
  .scenario-controls select { flex-basis: 100%; width: 100%; }
  .download-button { width: auto; }
  .upload-grid { grid-template-columns: 1fr; }
  .upload-card { min-height: 96px; padding: 17px; }
  .replace-label { display: none; }
  .editor-heading { align-items: stretch; flex-direction: column; }
  .editor-actions { width: 100%; flex-wrap: wrap; }
  .collapsible-heading { min-height: 64px; }
  .section-toggle strong { font-size: 17px; }
  .editor-toolbar { align-items: flex-start; flex-direction: column; }
  .result-heading { margin-top: 30px; }
  .summary-cards { display: grid; grid-template-columns: 1fr; }
  .summary-card { min-width: 0; }
  #summary-table th:nth-child(2), #summary-table td:nth-child(2) { left: var(--summary-first-width, 72px); }
  .admin-create-form { grid-template-columns: 1fr; }
  .admin-table-scroll { margin-inline: -8px; }
}

@media (min-width: 721px) and (max-width: 1380px) {
  .topbar { flex-wrap: wrap; }
  .top-kpis { order: 3; max-width: none; flex-basis: 100%; }
}
