:root {
  --bg: #FAF8F3;
  --surface: #FFFFFF;
  --surface-soft: #F6F2E9;
  --border: #E8E2D3;
  --border-strong: #D6CFBE;
  --ink: #1F1B16;
  --muted: #6E6659;
  --accent: #1E3A8A;
  --accent-hover: #1E40AF;
  --positive: #166534;
  --warning: #B45309;
  --danger: #991B1B;
  --danger-soft: #FCE8E6;
  --shadow-sm: 0 1px 2px rgba(31,27,22,0.04);
  --shadow-md: 0 4px 12px rgba(31,27,22,0.06), 0 1px 2px rgba(31,27,22,0.04);
  --shadow-lg: 0 20px 40px rgba(31,27,22,0.12), 0 4px 12px rgba(31,27,22,0.06);
  --radius: 6px;
  --radius-lg: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 14px;
  color: var(--ink);
  background: var(--bg);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
  margin: 0;
  font-family: 'Fraunces', Georgia, serif;
  font-weight: 500;
  font-variation-settings: "opsz" 24, "SOFT" 50;
  letter-spacing: -0.01em;
}
h1 { font-size: 1.375rem; }
h2 { font-size: 1.15rem; font-weight: 500; }
h3 { font-size: 0.95rem; margin: 1.5rem 0 0.625rem; color: var(--muted); font-weight: 500; font-style: italic; }

.num, .num * { font-family: 'IBM Plex Mono', ui-monospace, monospace; font-variant-numeric: tabular-nums; }

.container { max-width: 1180px; margin: 0 auto; padding: 0 1.5rem; }
.main { padding-bottom: 4rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.hidden { display: none !important; }
.text-right { text-align: right; }

/* Header */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.875rem 0;
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: saturate(180%) blur(8px);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: var(--ink);
  color: var(--bg);
  border-radius: var(--radius);
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 0.85rem;
  letter-spacing: 0.02em;
}

.header-actions { display: flex; gap: 0.375rem; flex-wrap: wrap; }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 500;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink);
  padding: 0.5rem 0.875rem;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn:hover { background: var(--surface-soft); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--surface-soft); }

.btn-danger { color: var(--danger); border-color: var(--border-strong); background: var(--surface); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }

.btn-sm { padding: 0.25rem 0.55rem; font-size: 0.82rem; }

/* Summary */
.summary-section { margin: 1.75rem 0 1rem; }

.grant-selector {
  display: flex;
  align-items: baseline;
  gap: 0.875rem;
  margin-bottom: 1rem;
}

.grant-selector label {
  font-family: 'Fraunces', serif;
  font-style: italic;
  color: var(--muted);
  font-size: 0.95rem;
}

.grant-selector select {
  flex: 1;
  max-width: 560px;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  font: inherit;
}

.summary-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.75rem;
}

.summary-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1rem 1.25rem 1.125rem;
  box-shadow: var(--shadow-sm);
}

.summary-card .label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 500;
}

.summary-card .value {
  font-family: 'IBM Plex Mono', monospace;
  font-variant-numeric: tabular-nums;
  font-size: 1.5rem;
  font-weight: 500;
  margin-top: 0.375rem;
  letter-spacing: -0.01em;
}

.summary-card .value.positive { color: var(--positive); }
.summary-card .value.warning  { color: var(--warning); }
.summary-card .value.negative { color: var(--danger); }

.progress {
  height: 5px;
  background: var(--surface-soft);
  border-radius: 999px;
  margin-top: 0.75rem;
  overflow: hidden;
}
.progress-bar { height: 100%; background: var(--accent); transition: width 0.4s ease; }
.progress-bar.warning { background: var(--warning); }
.progress-bar.danger  { background: var(--danger); }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.375rem 1.5rem;
  margin: 1rem 0;
  box-shadow: var(--shadow-sm);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

/* Forms */
.form-row { display: flex; gap: 0.75rem; margin-bottom: 0.75rem; flex-wrap: wrap; }

.form-field { display: flex; flex-direction: column; gap: 0.3rem; min-width: 140px; }
.form-field-grow { flex: 1; min-width: 200px; }

.form-field label {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 500;
  letter-spacing: 0.02em;
}

input[type="text"],
input[type="number"],
input[type="date"],
input[type="password"],
select {
  font: inherit;
  padding: 0.5rem 0.625rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  width: 100%;
}
input[type="number"] { font-family: 'IBM Plex Mono', monospace; }

input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(30,58,138,0.12);
}

.form-actions { display: flex; gap: 0.5rem; margin-top: 0.5rem; align-items: center; }

.filter-bar input { min-width: 240px; }

/* Table */
.table-wrap { overflow-x: auto; margin: 0 -0.25rem; }

table { width: 100%; border-collapse: collapse; }

th, td { padding: 0.65rem 0.75rem; text-align: left; border-bottom: 1px solid var(--border); }

th {
  font-weight: 500;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  white-space: nowrap;
}

tbody tr { transition: background 0.1s; }
tbody tr:hover { background: var(--surface-soft); }

td.num { font-family: 'IBM Plex Mono', monospace; font-variant-numeric: tabular-nums; }

tfoot td {
  background: var(--surface-soft);
  border-bottom: none;
  border-top: 1px solid var(--border-strong);
  padding-top: 0.75rem;
  padding-bottom: 0.75rem;
}

.empty-state {
  padding: 2.5rem;
  text-align: center;
  color: var(--muted);
  font-style: italic;
  font-family: 'Fraunces', serif;
}

.cat-badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.72rem;
  background: var(--surface-soft);
  color: var(--muted);
  border: 1px solid var(--border);
  letter-spacing: 0.02em;
}

.row-actions { display: flex; gap: 0.25rem; justify-content: flex-end; }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(31,27,22,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
  backdrop-filter: blur(2px);
}

.modal-content {
  background: var(--surface);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 720px;
  max-height: 90vh;
  overflow: auto;
  box-shadow: var(--shadow-lg);
}

.modal-sm { max-width: 620px; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.375rem;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
}

.modal-close {
  font-size: 1.5rem;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  line-height: 1;
  padding: 0 0.25rem;
}
.modal-close:hover { color: var(--ink); }

.modal-body { padding: 1.25rem 1.375rem 1.5rem; }

.grants-list { display: flex; flex-direction: column; gap: 0.5rem; }

.grant-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.875rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  gap: 1rem;
}

.grant-item-info { flex: 1; min-width: 0; }
.grant-item-name { font-weight: 500; font-family: 'Fraunces', serif; font-size: 1.02rem; }
.grant-item-meta { font-size: 0.82rem; color: var(--muted); margin-top: 0.2rem; }
.grant-item-meta .num { font-size: 0.82rem; }
.grant-item-actions { display: flex; gap: 0.375rem; flex-shrink: 0; }

/* Login */
.login-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: var(--bg);
  background-image: radial-gradient(ellipse at top, rgba(30,58,138,0.04), transparent 60%);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 2.25rem;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow-md);
  text-align: center;
}

.login-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  background: var(--ink);
  color: var(--bg);
  border-radius: var(--radius);
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 1.05rem;
  margin-bottom: 1rem;
}

.login-card h1 { font-size: 1.625rem; margin-bottom: 0.375rem; }

.login-card form {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  margin-top: 1.5rem;
}

.login-card input { text-align: center; padding: 0.65rem; }

.login-card button {
  background: var(--accent);
  color: white;
  border: none;
  padding: 0.65rem;
  border-radius: var(--radius);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}
.login-card button:hover { background: var(--accent-hover); }

.error { color: var(--danger); font-size: 0.85rem; min-height: 1.25rem; }

/* Tabs */
.tabbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 60px;
  z-index: 9;
}
.tabs {
  display: flex;
  gap: 0.125rem;
  overflow-x: auto;
  padding: 0.25rem 0;
  scrollbar-width: thin;
}
.tab {
  flex-shrink: 0;
  font: inherit;
  font-size: 0.88rem;
  font-weight: 500;
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 0.625rem 0.875rem;
  border-radius: var(--radius);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.12s ease;
}
.tab:hover { background: var(--surface-soft); color: var(--ink); }
.tab.active {
  background: var(--ink);
  color: var(--bg);
}
.tab .count {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.72rem;
  opacity: 0.7;
  margin-left: 0.35rem;
}

/* Sheet meta strip */
.sheet-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin: 1.25rem 0 0.75rem;
  flex-wrap: wrap;
}
.sheet-title-wrap { display: flex; align-items: baseline; gap: 0.75rem; }
.sheet-title {
  font-family: 'Fraunces', serif;
  font-size: 1.35rem;
}

/* Grid */
.grid-card { padding: 0; overflow: hidden; }
.grid-wrap {
  overflow: auto;
  max-height: calc(100vh - 240px);
}
#grid {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  min-width: 100%;
}
#grid thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface-soft);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border-strong);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 600;
  padding: 0.6rem 0.65rem;
  white-space: nowrap;
  overflow: visible;
}
/* Absolute positioning inside sticky should work, but some Chromium versions
   have edge cases. Force a relative containing block as a safety net. */
#grid thead th { position: sticky; }
.col-resize-handle {
  position: absolute;
  right: -5px;        /* hangs over the column boundary; easier to grab */
  top: 0;
  bottom: 0;
  width: 10px;
  cursor: col-resize;
  user-select: none;
  background: transparent;
  z-index: 5;
  touch-action: none;
}
/* A subtle always-visible grip in the middle of the handle. */
.col-resize-handle::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 25%;
  bottom: 25%;
  width: 2px;
  margin-left: -1px;
  background: rgba(31, 27, 22, 0.12);
  border-radius: 1px;
}
.col-resize-handle:hover { background: rgba(30, 58, 138, 0.18); }
.col-resize-handle:hover::after { background: rgba(30, 58, 138, 0.65); }
.col-resize-handle:active { background: rgba(30, 58, 138, 0.30); }
body.col-resizing { cursor: col-resize !important; user-select: none; }
body.col-resizing * { cursor: col-resize !important; }
#grid thead th.col-action { width: 38px; min-width: 38px; }
#grid thead th.col-rownum {
  width: 44px;
  min-width: 44px;
  text-align: right;
  position: sticky;
  left: 0;
  z-index: 3;
}
#grid tbody td {
  padding: 0;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
  background: var(--surface);
}
#grid tbody tr:hover td { background: var(--surface-soft); }
#grid tbody td.row-num {
  background: var(--surface-soft);
  color: var(--muted);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.78rem;
  text-align: right;
  padding: 0.5rem 0.5rem;
  position: sticky;
  left: 0;
  z-index: 1;
  border-right: 1px solid var(--border);
}
.cell-input {
  width: 100%;
  border: none;
  background: transparent;
  padding: 0.45rem 0.65rem;
  font: inherit;
  font-size: 0.88rem;
  color: var(--ink);
  outline: none;
  min-width: 80px;
}
.cell-input.numeric {
  font-family: 'IBM Plex Mono', monospace;
  font-variant-numeric: tabular-nums;
  text-align: right;
}
.cell-input:focus {
  background: #FFF8E0;
  box-shadow: inset 0 0 0 2px var(--accent);
}
.cell-input.dirty { background: #FFFBE6; }
.row-del {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 0.4rem 0.5rem;
  font-size: 0.95rem;
}
.row-del:hover { color: var(--danger); background: var(--danger-soft); }

.feedback { margin-top: 0.75rem; min-height: 1.1rem; }
.feedback.ok { color: var(--positive); }
.feedback.err { color: var(--danger); }

/* Dashboard layout */
.dash-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr);
  gap: 1rem;
  margin-top: 1rem;
}
.chart-card {
  padding: 1rem 1.125rem 1.25rem;
}
.chart-card h3 {
  margin: 0 0 0.75rem;
  color: var(--ink);
  font-style: normal;
  font-family: 'Fraunces', serif;
  font-size: 1rem;
  font-weight: 500;
}
.chart-wrap {
  position: relative;
  height: 360px;
}
@media (max-width: 920px) {
  .dash-grid { grid-template-columns: 1fr; }
}

.btn[data-needs-sheet][disabled],
.btn[data-needs-sheet]:disabled,
.btn[disabled],
.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.backup-status {
  font-size: 0.68rem;
  letter-spacing: 0.02em;
  padding: 0.25rem 0 0;
  color: var(--muted);
}
.backup-status.stale { color: var(--warning); }

/* Useful-commands card on dashboard */
.help-card {
  margin-top: 1rem;
  padding: 1.25rem 1.5rem 1.5rem;
}
.help-card h3 {
  margin: 0 0 0.875rem;
  color: var(--ink);
  font-family: 'Fraunces', serif;
  font-style: normal;
  font-weight: 500;
  font-size: 1.05rem;
}
.help-list {
  margin: 0;
  display: grid;
  grid-template-columns: minmax(190px, 1fr) minmax(0, 2.4fr);
  gap: 0.6rem 1rem;
  font-size: 0.88rem;
}
.help-list dt {
  font-weight: 600;
  color: var(--ink);
  align-self: start;
  padding-top: 0.05rem;
}
.help-list dd {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
}
.help-list em { color: var(--ink); font-style: normal; }
.help-list code {
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-size: 0.82rem;
  background: var(--surface-soft);
  padding: 0.05rem 0.3rem;
  border-radius: 3px;
  border: 1px solid var(--border);
}
.kbd {
  display: inline-block;
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-size: 0.75rem;
  background: var(--surface-soft);
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  padding: 0.05rem 0.4rem;
  border-radius: 4px;
  color: var(--ink);
  line-height: 1.3;
}
@media (max-width: 720px) {
  .help-list { grid-template-columns: 1fr; gap: 0.25rem 0; }
  .help-list dt { padding-top: 0.6rem; }
}

/* Range selection highlight */
#grid tbody td.cell-selected {
  background: rgba(30, 58, 138, 0.10);
  box-shadow: inset 0 0 0 1px var(--accent);
}
#grid tbody tr:hover td.cell-selected { background: rgba(30, 58, 138, 0.15); }

/* Context menu (right-click on row number or column header) */
.context-menu {
  position: fixed;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 0.25rem;
  z-index: 1000;
  min-width: 200px;
  font-size: 0.88rem;
}
.context-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  padding: 0.5rem 0.75rem;
  font: inherit;
  cursor: pointer;
  border-radius: 4px;
  color: var(--ink);
}
.context-item:hover { background: var(--surface-soft); }
.context-item.danger { color: var(--danger); }
.context-item.danger:hover { background: var(--danger-soft); }

/* Pinned totals row (top of certain sheets) */
#grid tbody tr.totals-row td {
  position: sticky;
  top: 38px;          /* below the sticky header */
  z-index: 1;
  background: #ECE7DA;
  color: var(--muted);
  border-bottom: 2px solid var(--border-strong);
  padding: 0.55rem 0.65rem;
  font-weight: 500;
  user-select: none;
}
#grid tbody tr.totals-row td.row-num {
  background: #DDD6C2;
  color: var(--muted);
  z-index: 2;
}
#grid tbody tr.totals-row td.totals-num {
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--ink);
  text-align: right;
}
#grid tbody tr.totals-row td.col-action { background: #ECE7DA; }
#grid tbody tr.totals-row:hover td { background: #E2DBC8; }
#grid tbody tr.totals-row:hover td.totals-num { background: #E2DBC8; }

@media (max-width: 720px) {
  .container { padding: 0 1rem; }
  .header-actions { width: 100%; justify-content: flex-end; }
  .form-field { min-width: 100%; }
  th, td { padding: 0.5rem 0.5rem; font-size: 0.85rem; }
  h1 { font-size: 1.2rem; }
  .summary-card .value { font-size: 1.25rem; }
  .grid-wrap { max-height: calc(100vh - 280px); }
}
