:root {
  /* main area */
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #fafafa;
  --text: #1d1d1f;
  --text-muted: #6e6e73;
  --text-faint: #a1a1a6;
  --label: #8a8a8e;
  --border: #ececec;
  --border-mid: #e0e0e0;
  --border-strong: #dcdcdc;
  --divider: #efefef;
  --accent: #6b8cba;
  --accent-strong: #567099;
  --accent-soft: #eef2f7;
  --danger: #b5483f;

  /* sidebar */
  --side-bg: #1e1e1e;
  --side-text: #e6e6e6;
  --side-dim: #9a9a9a;
  --side-section: #555555;
  --side-hover: #2a2a2a;
  --side-active: #262626;
  --side-accent: #6b8cba;

  --radius: 4px;
  --mono: ui-monospace, "SF Mono", "SFMono-Regular", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ===================== Layout ===================== */
.layout { display: flex; height: 100vh; overflow: hidden; }

/* ===================== Sidebar ===================== */
.sidebar {
  width: 212px;
  flex-shrink: 0;
  background: var(--side-bg);
  color: var(--side-text);
  display: flex;
  flex-direction: column;
}
.side-brand { padding: 20px 18px 18px; }
.brand-mark { display: none; }
.brand-name { font-weight: 600; font-size: 16px; color: #fff; line-height: 1.1; letter-spacing: 0.2px; }
.brand-name small {
  display: block; margin-top: 4px;
  font-size: 10px; font-weight: 500; letter-spacing: 1.8px;
  text-transform: uppercase; color: var(--side-dim);
}

.side-nav { flex: 1; overflow-y: auto; padding: 4px 0 10px; scrollbar-width: none; -ms-overflow-style: none; }
.side-nav::-webkit-scrollbar { width: 0; height: 0; display: none; }

.nav-item {
  display: flex; align-items: center; gap: 9px;
  width: 100%; text-align: left;
  background: transparent; border: none;
  border-left: 2px solid transparent;
  color: var(--side-text);
  font-family: var(--sans); font-size: 13px;
  padding: 7px 18px; cursor: pointer;
}
.nav-item:hover { background: var(--side-hover); }
.nav-item.active { background: var(--side-active); border-left-color: var(--side-accent); color: #fff; }
.nav-ico { width: 14px; text-align: center; color: var(--side-dim); font-size: 12px; }
.nav-item.active .nav-ico { color: var(--side-accent); }

.nav-group-toggle {
  display: flex; align-items: center; gap: 7px;
  width: 100%; background: transparent; border: none; cursor: pointer;
  color: var(--side-section);
  font-size: 10px; text-transform: uppercase; letter-spacing: 1.1px;
  padding: 14px 18px 5px;
}
.nav-group-toggle:hover { color: #7a7a7a; }
.nav-group-toggle .nav-ico { color: var(--side-section); font-size: 9px; transition: transform 0.15s ease; }
.nav-group-toggle.collapsed .nav-ico { transform: rotate(-90deg); }
.nav-sub.collapsed { display: none; }
.nav-item.sub { padding-left: 34px; font-size: 13px; color: var(--side-dim); }
.nav-item.sub:hover { color: var(--side-text); }
.nav-item.sub.active { color: #fff; }
.month-item { font-variant-numeric: tabular-nums; }

.grp-year {
  margin-left: auto; color: var(--side-accent);
  font-size: 11px; letter-spacing: 0; text-transform: none;
  font-variant-numeric: tabular-nums; cursor: pointer; padding: 0 4px; border-radius: 3px;
}
.grp-year:hover { color: #fff; background: var(--side-hover); }

.side-bottom { border-top: 1px solid #2d2d2d; padding: 6px 0; }
.side-bottom .nav-item { color: var(--side-dim); }
.side-bottom .nav-item.active { color: #fff; }

/* ===================== Main ===================== */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: #fff; }
.main-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 15px 24px; border-bottom: 1px solid var(--border);
}
.main-head h1 { margin: 0; font-size: 17px; font-weight: 600; letter-spacing: 0.2px; }
.main-body { flex: 1; overflow-y: auto; }
.view { display: block; }
.view[hidden] { display: none; }

.head-actions { display: flex; align-items: center; gap: 8px; }

/* understated Print / Export button */
.btn-print {
  font-family: var(--sans); font-size: 12.5px;
  padding: 6px 13px; border: 1px solid var(--border-mid);
  background: #fff; color: var(--text); border-radius: var(--radius); cursor: pointer;
}
.btn-print:hover { background: var(--surface-2); border-color: #cfcfcf; }

/* ===================== Form bar (one line) ===================== */
.formbar {
  display: flex; align-items: flex-end; gap: 16px; flex-wrap: wrap;
  padding: 18px 24px; border-bottom: 1px solid var(--divider);
}
.f-freee { flex: 0 1 210px; min-width: 150px; }
.formbar.wrap { flex-wrap: wrap; }
.formbar.sub-row { background: var(--surface-2); padding-top: 14px; padding-bottom: 14px; }
.formbar[hidden] { display: none; }

.f { display: flex; flex-direction: column; gap: 4px; flex: 0 1 112px; min-width: 74px; }
.f > span { font-size: 10px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--label); }
.f-grow { flex: 1 1 130px; }
.f-date { flex: 0 0 132px; }
.f-amt { flex: 0 0 104px; }
.f-sm { flex: 0 0 66px; }
.f-start { flex: 0 1 158px; }
.f-inline { display: flex; gap: 6px; }
.f-inline select { width: auto; flex: 1 1 0; min-width: 0; }

.formbar input, .formbar select {
  width: 100%;
  font-family: var(--sans); font-size: 13.5px; color: var(--text);
  border: none; border-bottom: 1px solid var(--border-mid); border-radius: 0;
  background: transparent; padding: 5px 2px;
  appearance: none; -webkit-appearance: none;
}
.formbar select { appearance: none; -webkit-appearance: none; background-image: none; padding-right: 14px; }
.formbar input:focus, .formbar select:focus { outline: none; border-bottom-color: var(--accent); }
.formbar input.num { text-align: right; font-family: var(--mono); }
.formbar input::placeholder { color: var(--text-faint); }

.f-check {
  display: flex; align-items: center; gap: 6px;
  font-size: 12.5px; color: var(--text-muted);
  white-space: nowrap; padding-bottom: 6px;
}
.f-check input { margin: 0; accent-color: var(--accent); }

.formbar-actions { margin-left: auto; display: flex; align-items: center; gap: 10px; padding-bottom: 2px; }
.pd-total { font-family: var(--mono); font-weight: 600; font-size: 14px; padding: 5px 2px; }

/* Save button — subtle, not bubbly */
.btn-save {
  font-family: var(--sans); font-size: 12.5px; font-weight: 500;
  padding: 7px 16px; border: 1px solid var(--accent);
  background: var(--accent); color: #fff; border-radius: var(--radius); cursor: pointer;
  white-space: nowrap;
}
.btn-save:hover { background: var(--accent-strong); border-color: var(--accent-strong); }

/* form-staged receipt (paperclip in the form) */
.btn-attach-form {
  border: none; background: transparent; cursor: pointer;
  font-size: 15px; color: var(--text-faint); padding: 3px 4px; line-height: 1;
}
.btn-attach-form:hover { color: var(--accent); }
.attach-name { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-muted); }
.attach-file { color: var(--accent); max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.li-clip-x { border: none; background: transparent; cursor: pointer; color: var(--text-faint); font-size: 11px; padding: 2px 4px; }
.li-clip-x:hover { color: var(--danger); }

/* number spinners off everywhere */
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type="number"] { -moz-appearance: textfield; appearance: textfield; }

/* ===================== Settings ===================== */
.settings-block { padding: 20px 24px; border-bottom: 1px solid var(--divider); }
.block-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.9px; color: var(--text-faint); margin-bottom: 14px; }
.settings-block .formbar { padding: 0; border: none; gap: 20px; }

/* ===================== Expense list ===================== */
.list { padding: 6px 0 16px; }
.list-group + .list-group { margin-top: 4px; }
.group-label {
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.9px; color: var(--text-faint);
  padding: 14px 24px 6px;
}
.item-list { width: 100%; border-collapse: collapse; }
.item-list td { padding: 8px 10px; border-bottom: 1px solid var(--divider); vertical-align: middle; }
.item-list td:first-child { padding-left: 24px; }
.item-list td:last-child { padding-right: 24px; }
.item-list tr:hover { background: var(--surface-2); }
.list-empty { padding: 24px; text-align: center; color: var(--text-faint); font-style: italic; }

/* clickable rows in the month view (open the edit modal) */
.month-row { cursor: pointer; }
.month-row:hover { background: var(--surface-2); }
.month-row.recurring-row { cursor: pointer; }
.li-sub { color: var(--text-faint); font-size: 12px; }

/* + Add Expense */
.add-bar { padding: 18px 24px 26px; }
.btn-add-expense {
  font-family: var(--sans); font-size: 13px; font-weight: 500;
  padding: 9px 16px; border: 1px dashed var(--border-strong);
  background: #fff; color: var(--text-muted); border-radius: var(--radius);
  cursor: pointer; width: 100%;
}
.btn-add-expense:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

.li-rno { width: 46px; color: var(--text-faint); font-family: var(--mono); font-size: 12px; }
.li-date { width: 118px; }
.li-amt { width: 118px; text-align: right; }
.li-amt input { text-align: right; font-family: var(--mono); font-weight: 600; }
.month-row .li-amt { font-family: var(--mono); font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.month-row .li-main { color: var(--text); }
.month-row .li-date { color: var(--text-muted); font-size: 12.5px; }
/* Freee category tag on a month-view row */
.freee-tag {
  display: inline-block; font-size: 11px; color: var(--text-muted);
  border: 1px solid var(--border-mid); border-radius: 3px; padding: 0 6px; margin-left: 8px;
  vertical-align: middle;
}
/* recurring-row freee select cell */
.li-freee-cell { width: 150px; }
.li-receipt { width: 62px; text-align: center; white-space: nowrap; }
.li-sm-cell { width: 76px; }
.li-act { width: 40px; text-align: right; }
/* remove-receipt ✕ stays hidden until the row is hovered */
.item-list .li-clip-x { opacity: 0; }
.item-list tr:hover .li-clip-x { opacity: 0.6; }
.item-list .li-clip-x:hover { opacity: 1; }
.li-tag {
  display: inline-block; font-size: 10px; letter-spacing: 0.4px; text-transform: uppercase;
  color: var(--text-faint); border: 1px solid var(--border-mid); border-radius: 3px;
  padding: 0 5px; margin-left: 6px;
}

/* borderless inline inputs — look like text until hover/focus */
.item-list input, .item-list select {
  width: 100%; font-family: var(--sans); font-size: 13px; color: var(--text);
  border: none; border-bottom: 1px solid transparent; border-radius: 0;
  background: transparent; padding: 4px 2px;
  appearance: none; -webkit-appearance: none;
}
.item-list input[type="date"] { font-family: var(--mono); font-size: 12.5px; color: var(--text-muted); }
.item-list tr:hover input { border-bottom-color: var(--border-mid); }
.item-list input:focus, .item-list select:focus { outline: none; border-bottom-color: var(--accent); }
.item-list select { padding-right: 12px; }

.li-main .li-edit-line { display: flex; align-items: center; gap: 6px; }
.li-edit-line input { flex: 1; min-width: 0; }
.li-arrow { color: var(--text-faint); flex: 0 0 auto; }
.li-edit-reason { margin-top: 4px; }
.pd-inline { display: flex; align-items: center; gap: 6px; margin-top: 5px; font-size: 12px; color: var(--text-muted); flex-wrap: wrap; }
.pd-inline input[type="number"] { width: 54px; }
.pd-inline select { width: auto; }

/* receipt paperclip + delete: faint, brighten on row hover */
.li-clip { text-decoration: none; color: var(--accent); font-size: 14px; padding: 2px 4px; }
.li-attach {
  border: none; background: transparent; cursor: pointer;
  color: var(--text-faint); opacity: 0.4; font-size: 14px; padding: 2px 4px; line-height: 1;
}
.item-list tr:hover .li-attach { opacity: 0.8; }
.li-attach:hover { opacity: 1; color: var(--accent); }
.row-del {
  border: none; background: transparent; cursor: pointer;
  color: var(--text-faint); opacity: 0.3; font-size: 14px; padding: 2px 6px;
}
.item-list tr:hover .row-del { opacity: 0.65; }
.row-del:hover { opacity: 1; color: var(--danger); }
.row-edit {
  border: none; background: transparent; cursor: pointer;
  color: var(--text-faint); opacity: 0.35; font-size: 13px; padding: 2px 5px;
}
.item-list tr:hover .row-edit { opacity: 0.75; }
.row-edit:hover { opacity: 1; color: var(--accent); }
.sect-act { width: 66px; text-align: right; white-space: nowrap; }
.rc-since { font-size: 11px; color: var(--text-faint); white-space: nowrap; }

/* ===================== Bottom total bar ===================== */
.totalbar {
  display: none; align-items: center; justify-content: space-between;
  padding: 13px 24px; border-top: 1px solid var(--border-mid);
}
/* the month total only shows while viewing a month, not in an entry section */
body.viewing-month .totalbar { display: flex; }
.total-label { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--text-muted); }
.total-value { font-family: var(--mono); font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ===================== Toast ===================== */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: #1d1d1f; color: #fff; padding: 8px 16px; border-radius: var(--radius);
  font-size: 13px; z-index: 60; opacity: 0; transition: opacity 0.2s ease;
}
.toast.show { opacity: 0.96; }
.toast[hidden] { display: none; }

/* ===================== Modal (Print / Export) ===================== */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(28, 30, 34, 0.4);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  width: 100%; max-width: 440px; box-shadow: 0 16px 48px rgba(0, 0, 0, 0.18); overflow: hidden;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 15px 20px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; font-size: 15px; font-weight: 600; }
.icon-btn { border: none; background: transparent; cursor: pointer; color: var(--text-muted); font-size: 14px; padding: 4px 8px; border-radius: 3px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.modal-body { padding: 18px 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
.pm-section { margin-bottom: 18px; }
.pm-section:last-child { margin-bottom: 0; }
.pm-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.9px; color: var(--text-muted); margin-bottom: 9px; }
.pm-range { display: flex; align-items: flex-end; gap: 10px; }
.pm-range label { display: flex; flex-direction: column; gap: 4px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--label); flex: 1; }
.pm-range input {
  border: 1px solid var(--border-mid); border-radius: var(--radius);
  padding: 6px 8px; font-size: 13px; font-family: var(--mono); color: var(--text); background: #fff;
}
.pm-range input:focus { outline: none; border-color: var(--accent); }
.pm-arrow { color: var(--text-faint); padding-bottom: 8px; }
.pm-cats { display: flex; flex-wrap: wrap; gap: 8px 18px; }
.pm-chk { display: flex; align-items: center; gap: 6px; font-size: 13.5px; cursor: pointer; }
.pm-chk input { margin: 0; accent-color: var(--accent); }

/* ===================== Add / edit expense modal ===================== */
.em-field { margin-bottom: 14px; }
.em-field > label {
  display: block; font-size: 10px; text-transform: uppercase; letter-spacing: 0.6px;
  color: var(--label); margin-bottom: 5px;
}
.em-field input[type="text"], .em-field input[type="number"], .em-field input[type="date"], .em-field select {
  width: 100%; font-family: var(--sans); font-size: 13.5px; color: var(--text);
  border: 1px solid var(--border-mid); border-radius: var(--radius);
  background: #fff; padding: 7px 9px; appearance: none; -webkit-appearance: none;
}
.em-field input.num { text-align: right; font-family: var(--mono); }
.em-field input:focus, .em-field select:focus { outline: none; border-color: var(--accent); }
.em-row { display: flex; gap: 12px; }
.em-row .em-field { flex: 1; }
.em-total { font-family: var(--mono); font-weight: 600; font-size: 15px; padding: 6px 2px; }

.em-types { display: flex; gap: 6px; }
.em-types button {
  flex: 1; font-family: var(--sans); font-size: 12.5px;
  padding: 7px 6px; border: 1px solid var(--border-mid); background: #fff;
  color: var(--text-muted); border-radius: var(--radius); cursor: pointer;
}
.em-types button:hover { background: var(--surface-2); }
.em-types button.active { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-strong); font-weight: 600; }
.em-types.locked button:not(.active) { opacity: 0.4; cursor: default; }
.em-type-fields[hidden] { display: none; }
.em-check { display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--text-muted); cursor: pointer; }
.em-check[hidden] { display: none; }
.em-check input { accent-color: var(--accent); }
.em-receipt { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 13px; color: var(--text-muted); min-height: 20px; }
.em-receipt .li-clip { font-size: 13px; }
.em-foot-spacer { flex: 1; }
.btn-mini {
  font-family: var(--sans); font-size: 12px; padding: 5px 11px;
  border: 1px solid var(--border-mid); background: #fff; color: var(--text-muted);
  border-radius: var(--radius); cursor: pointer;
}
.btn-mini:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { border-color: #e2c4c1; color: var(--danger); }
.btn-danger:hover { background: #f7ecea; color: var(--danger); }

/* ===================== Year picker popover ===================== */
.year-menu {
  position: fixed; z-index: 70;
  background: #2a2a2a; border: 1px solid #3a3a3a; border-radius: var(--radius);
  padding: 4px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  display: flex; flex-direction: column; min-width: 84px;
}
.year-menu[hidden] { display: none; }
.year-opt {
  background: transparent; border: none; color: var(--side-text);
  font-family: var(--sans); font-size: 13px; font-variant-numeric: tabular-nums;
  text-align: left; padding: 6px 12px; border-radius: 3px; cursor: pointer;
}
.year-opt:hover { background: #363636; }
.year-opt.active { color: #fff; background: var(--side-accent); }

.btn { font-family: var(--sans); font-size: 13px; padding: 7px 15px; border-radius: var(--radius); border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); cursor: pointer; }
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn-ghost { background: transparent; }

/* ===================== Invoice ===================== */
.invoice-wrap { display: none; }
body.previewing .layout { display: none; }
body.previewing .invoice-wrap { display: block; height: 100vh; overflow-y: auto; background: var(--surface-2); }
.invoice-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 28px; background: #fff; border-bottom: 1px solid var(--border); position: sticky; top: 0;
}
.invoice {
  max-width: 780px; margin: 32px auto; background: #fff;
  padding: 56px 60px; border: 1px solid var(--border); box-shadow: 0 1px 6px rgba(0, 0, 0, 0.05);
}
.inv-header { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 2px solid var(--text); padding-bottom: 18px; margin-bottom: 26px; }
.inv-title { font-size: 24px; font-weight: 700; margin: 0; letter-spacing: 0.5px; }
.inv-sub { font-size: 13px; color: var(--text-muted); margin-top: 4px; }
.inv-meta { text-align: right; font-size: 13px; line-height: 1.7; }
.inv-meta .inv-month { font-size: 16px; font-weight: 600; }
.inv-meta .label { color: var(--text-muted); }
.inv-group { margin-bottom: 22px; }
.inv-group h4 { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--text-muted); margin: 0 0 6px; padding-bottom: 4px; border-bottom: 1px solid var(--border); }
.inv-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.inv-table td { padding: 5px 8px; border-bottom: 1px solid #f0eee9; vertical-align: baseline; white-space: nowrap; }
.inv-table .r-no { width: 44px; color: var(--text-muted); font-family: var(--mono); }
.inv-table .r-date { width: 96px; color: var(--text-muted); font-family: var(--mono); font-size: 12px; }
.inv-table .r-desc { white-space: normal; color: var(--text); }
.inv-table .r-extra { color: var(--text-muted); font-size: 12px; }
.inv-table .r-freee { color: var(--text-muted); font-size: 11.5px; }
.inv-table .r-amt { text-align: right; width: 110px; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.inv-table .r-clip { color: var(--text-faint); font-size: 11px; }
.inv-total { display: flex; justify-content: space-between; align-items: baseline; margin-top: 30px; padding-top: 16px; border-top: 2px solid var(--text); }
.inv-total .lbl { font-size: 13px; text-transform: uppercase; letter-spacing: 1px; }
.inv-total .val { font-family: var(--mono); font-size: 22px; font-weight: 700; }
.inv-empty { color: var(--text-faint); font-style: italic; text-align: center; padding: 40px 0; }
.inv-signoff { margin-top: 48px; display: flex; justify-content: space-between; gap: 40px; font-size: 12px; color: var(--text-muted); }
.inv-sign-line { flex: 1; }
.inv-sign-line .line { border-bottom: 1px solid var(--border-strong); height: 34px; margin-bottom: 4px; }

/* ===================== Print ===================== */
@media print {
  body { background: #fff; }
  .layout, .invoice-toolbar, .toast, .modal-backdrop { display: none !important; }
  .invoice-wrap { display: block !important; }
  .invoice { margin: 0; padding: 0; border: none; box-shadow: none; max-width: none; }
  .inv-table td { border-bottom: 1px solid #ddd; }
}

@media (max-width: 820px) {
  .formbar { flex-wrap: wrap; }
  .invoice { padding: 28px 20px; }
}
