: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 { 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: 12px; }

.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; }

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

.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 {
  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; }

.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: 34px; }
.editor-heading { display: flex; align-items: flex-end; 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; margin-top: 17px; padding: 3px 0 8px; 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: 1px solid var(--line); border-radius: 16px; background: var(--surface); box-shadow: var(--shadow); }
.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: 98px; padding: 6px 9px; text-align: left; }
.workbook-table thead th { background: #edf2f8; text-align: left; }
.workbook-table th:first-child, .workbook-table td:first-child { left: 0; 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) { left: 52px; min-width: 130px; max-width: none; }
.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-heading { margin-top: 40px; align-items: flex-end; }
.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: 17px;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.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; }
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; }
th:first-child, td:first-child { position: sticky; left: 0; z-index: 3; min-width: 104px; text-align: left; }
th:nth-child(2), td:nth-child(2) { position: sticky; left: 104px; z-index: 3; min-width: 220px; max-width: 280px; 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; }

.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 { align-items: flex-start; padding: 12px 16px; }
  .topbar-actions { align-items: flex-end; flex-direction: column-reverse; }
  .status-pill { max-width: 190px; }
  .page-shell { width: min(100% - 24px, 1680px); padding-top: 25px; }
  .intro-row { align-items: flex-start; flex-direction: column; }
  .download-button { width: 100%; }
  .upload-grid { grid-template-columns: 1fr; }
  .upload-card { min-height: 96px; padding: 17px; }
  .replace-label { display: none; }
  .editor-heading { align-items: flex-start; flex-direction: column; }
  .editor-actions { width: 100%; flex-wrap: wrap; }
  .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; }
  th:first-child, td:first-child { min-width: 76px; }
  th:nth-child(2), td:nth-child(2) { left: 76px; min-width: 200px; }
}
