:root {
  --accent: #1d4e4b;
  --primary: #2e1f1a;
  --secondary: #f4efe6;
  --ink: #3a2b22;
  --paper: #f6f0e2;
  --paper-edge: #e8dcc4;
  --gold: #b08d3f;
  --gold-hi: #d9b45c;
  --brass: #8a6d2f;
  --emerald: #1d6e5a;
  --amber: #b5812e;
  --crimson: #a12f2f;
  --cobalt: #2f4f8f;
  --green-wax: #2f7d44;
  --red-wax: #b03a2e;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: var(--primary);
}

/* Mahogany desk surface with leather grain */
.desk {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(120% 90% at 50% 0%, #4a3326 0%, #3a2a1f 40%, #2c1f18 100%);
  padding-bottom: calc(env(safe-area-inset-bottom, 0px));
}

.desk::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.10;
  background-image:
    repeating-linear-gradient(0deg, rgba(0,0,0,0.4) 0 2px, transparent 2px 7px),
    repeating-linear-gradient(90deg, rgba(255,255,255,0.03) 0 1px, transparent 1px 6px),
    radial-gradient(circle at 30% 40%, rgba(0,0,0,0.5) 0, transparent 60%),
    radial-gradient(circle at 70% 60%, rgba(0,0,0,0.35) 0, transparent 55%);
  mix-blend-mode: multiply;
}

/* ============ LEDGER PAGE ============ */
.ledger {
  position: relative;
  flex: 1;
  min-height: 0;
  margin: 10px;
  background: var(--paper);
  background-image:
    linear-gradient(90deg, rgba(180,150,110,0.06) 1px, transparent 1px),
    radial-gradient(ellipse at 30% 20%, rgba(255,255,255,0.6), transparent 60%),
    radial-gradient(ellipse at 70% 80%, rgba(200,170,120,0.18), transparent 70%);
  background-size: 22px 100%, 100% 100%, 100% 100%;
  border-radius: 6px;
  box-shadow:
    0 8px 30px rgba(0,0,0,0.6),
    inset 0 0 0 1px rgba(255,255,255,0.35),
    inset 0 0 60px rgba(120,90,50,0.12);
  overflow: hidden;
}

/* Leather spine border (left side) */
.ledger::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 14px;
  background: linear-gradient(90deg, #1a1410, #3a2a1d 40%, #241a12);
  background-image: repeating-linear-gradient(0deg, rgba(255,255,255,0.04) 0 3px, transparent 3px 9px);
  box-shadow: 2px 0 6px rgba(0,0,0,0.35);
  border-right: 1px solid #d8c9a8;
  z-index: 2;
}

/* Red edge lines of ledger page */
.ledger::after {
  content: "";
  position: absolute;
  left: 16px;
  right: 0;
  top: 0;
  height: 4px;
  background: linear-gradient(90deg, #c0392b, #e8c7b0 60%, transparent);
  opacity: 0.35;
  z-index: 2;
}

.page-scroll {
  position: absolute;
  inset: 0;
  padding: 20px 16px 40px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.page-scroll::-webkit-scrollbar { display: none; }

.page { display: none; }
.page.active { display: block; }

/* ============ COUNTERS ============ */
.counters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-bottom: 22px;
}

.counter {
  background: rgba(255,255,255,0.5);
  border: 1px solid var(--paper-edge);
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 2px 8px rgba(90,60,30,0.18);
  text-align: center;
}

.counter-label {
  display: block;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 12px;
  letter-spacing: 0.5px;
  color: #8a6d3f;
  text-transform: uppercase;
  margin-bottom: 8px;
}

/* Abacus */
.abacus {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 8px;
  background: linear-gradient(180deg, #5a3d24, #3a2a1b);
  border-radius: 8px;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.6), 0 1px 2px rgba(0,0,0,0.4);
  min-height: 30px;
  justify-content: center;
  flex-wrap: wrap;
}
.bead {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  box-shadow: inset -2px -2px 3px rgba(0,0,0,0.4), 0 1px 2px rgba(0,0,0,0.5);
  transition: background 0.3s, transform 0.2s;
}
.bead.unpaid { background: radial-gradient(circle at 35% 30%, #7fd49a, var(--green-wax)); }
.bead.overdue { background: radial-gradient(circle at 35% 30%, #f3a89b, var(--red-wax)); transform: scale(1.15); }
.bead.off { background: #6b5a48; opacity: 0.6; }

/* Rubber stamp */
.stamp {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--red-wax);
  border-radius: 4px;
  padding: 8px 14px;
  border: 2px solid #8f2f24;
  box-shadow: 2px 2px 0 #7a251c, 0 4px 12px rgba(176,58,46,0.4);
  transform: rotate(-4deg);
}
.stamp-num {
  font-family: Georgia, serif;
  font-weight: bold;
  font-size: 26px;
  color: #fff;
  text-shadow: 0 1px 0 rgba(0,0,0,0.3);
}

/* ============ RECENT ============ */
.recent-title, .form-title {
  font-family: Georgia, "Times New Roman", serif;
  color: #7a5c2e;
  font-size: 16px;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  border-bottom: 1px dashed #c9b98d;
  padding-bottom: 6px;
  margin: 6px 0 14px;
}
.form-title { margin-top: 0; }

/* ============ PARCHMENT SLIP ============ */
.slip {
  position: relative;
  background: #fdf7e8;
  padding: 14px 16px;
  margin-bottom: 12px;
  border: 1px solid #e5d6b6;
  box-shadow: 0 3px 10px rgba(90,60,30,0.22), inset 0 0 20px rgba(200,170,120,0.12);
  clip-path: polygon(
    0 4%, 2% 0, 5% 3%, 12% 0, 18% 3%, 30% 0, 44% 2%, 55% 0, 66% 3%,
    80% 0, 92% 2%, 100% 0, 100% 12%, 98% 20%, 100% 30%, 98% 44%,
    100% 58%, 98% 70%, 100% 82%, 98% 94%, 100% 100%, 88% 97%,
    72% 100%, 58% 97%, 44% 100%, 30% 97%, 16% 100%, 6% 98%, 0 100%,
    1% 88%, 0 74%, 2% 58%, 0 40%, 2% 22%, 0 10%
  );
  user-select: none;
  -webkit-user-select: none;
}
.slip:active { transform: scale(0.99); }

.slip-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}
.slip-name {
  font-family: "Segoe Script", "Brush Script MT", cursive;
  font-size: 20px;
  color: #4a3a26;
  flex: 1;
  overflow-wrap: anywhere;
}
.slip-amount {
  font-family: Georgia, "Times New Roman", serif;
  font-weight: bold;
  font-size: 20px;
  color: #2e1f1a;
  white-space: nowrap;
}
.slip-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 6px;
}
.slip-date {
  font-size: 12px;
  color: #9a8a6a;
  font-family: "Segoe UI", system-ui, sans-serif;
  letter-spacing: 0.3px;
}

/* Wax seal */
.seal {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  position: relative;
  box-shadow: 0 1px 4px rgba(0,0,0,0.35), inset 0 0 3px rgba(0,0,0,0.3);
  flex: 0 0 auto;
}
.seal::after {
  content: "";
  position: absolute;
  inset: 6px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.35);
}
.seal.seal-paid { background: radial-gradient(circle at 35% 30%, #6fbd82, var(--green-wax)); }
.seal.seal-overdue { background: radial-gradient(circle at 35% 30%, #ef9c8e, var(--red-wax)); }
.seal.seal-sent { background: radial-gradient(circle at 35% 30%, #f5cf8a, var(--amber)); }
.seal.seal-draft { background: transparent; box-shadow: none; }
.seal.seal-draft::after { display: none; }

/* ============ EMPTY STATE ============ */
.empty-state {
  text-align: center;
  padding: 50px 24px;
  color: #b5a37e;
}
.empty-state .quill {
  display: inline-block;
  width: 56px;
  height: 56px;
  position: relative;
  margin-bottom: 12px;
  border-radius: 50%;
}
.empty-state .quill::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
  width: 20px;
  height: 44px;
  background: linear-gradient(90deg, #6b4a2a, #9a743e);
  clip-path: polygon(50% 0, 62% 30%, 68% 60%, 50% 100%, 32% 60%, 38% 30%);
}
.empty-state .quill::after {
  content: "";
  position: absolute;
  bottom: -4px;
  left: 50%;
  transform: translateX(-50%);
  width: 5px;
  height: 10px;
  background: var(--accent);
  border-radius: 50%;
  filter: drop-shadow(0 0 3px rgba(29,78,75,0.5));
}
.empty-state p {
  font-family: Georgia, serif;
  font-style: italic;
  font-size: 15px;
  margin: 0;
}

/* ============ FORM ============ */
.field { display: block; margin-bottom: 18px; }
.field-label {
  display: block;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: #7a5c2e;
  margin-bottom: 6px;
}
.field input[type="text"],
.field input[type="number"],
.field input[type="date"],
.field select,
.search-input {
  width: 100%;
  font-size: 17px;
  padding: 14px;
  border: 1px solid #d6c49a;
  border-radius: 8px;
  background: #fffdf6;
  color: var(--ink);
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
}
.field input:focus, .field select:focus, .search-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(29,78,75,0.18);
}
.field select {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8'><path d='M1 1l5 5 5-5' stroke='%237a5c2e' stroke-width='2' fill='none'/></svg>");
  background-repeat: no-repeat;
  background-position: right 14px center;
}
.amount-wrap { position: relative; }
.amount-wrap .currency {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  font-family: Georgia, serif;
  font-weight: bold;
  font-size: 18px;
  color: #7a5c2e;
}
.amount-wrap input { padding-left: 32px; }
.field-error {
  display: block;
  color: var(--red-wax);
  font-size: 12px;
  min-height: 0;
  margin-top: 4px;
}
.field-error::before { content: ""; }

.form-actions { margin-top: 8px; display: flex; gap: 10px; }
.btn-primary, .btn-ghost {
  flex: 1;
  font-size: 17px;
  padding: 15px;
  border-radius: 10px;
  border: none;
  font-family: inherit;
  cursor: pointer;
}
.btn-primary {
  background: linear-gradient(180deg, var(--accent), #163b38);
  color: #f0ead8;
  box-shadow: 0 3px 8px rgba(0,0,0,0.3);
}
.btn-primary:active { transform: translateY(1px); }
.btn-ghost {
  background: transparent;
  border: 1px solid #c9b98d;
  color: #7a5c2e;
}

/* ============ SEARCH ============ */
.search-input { margin-bottom: 16px; }

/* ============ BOTTOM TAB BAR ============ */
.tabbar {
  flex: 0 0 auto;
  position: relative;
  display: flex;
  justify-content: space-around;
  align-items: flex-end;
  padding: 14px 8px calc(10px + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(180deg, #8a6d2f, #5f4718);
  border-top: 2px solid #caa84f;
  box-shadow: 0 -3px 12px rgba(0,0,0,0.5);
}
.tabbar::before {
  content: "";
  position: absolute;
  left: 0; right: 0; top: 0; bottom: 0;
  pointer-events: none;
  background: repeating-linear-gradient(90deg, rgba(255,255,255,0.06) 0 2px, transparent 2px 6px);
}

.inkwell {
  position: relative;
  flex: 1;
  max-width: 84px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 4px 0 0;
}

/* Well body */
.inkwell .ink {
  position: relative;
  width: 40px;
  height: 34px;
  border-radius: 45% 45% 50% 50% / 40% 40% 90% 90%;
  background: #1a130d;
  box-shadow: inset 0 -4px 6px rgba(0,0,0,0.7), 0 4px 6px rgba(0,0,0,0.4);
  transition: background 0.3s;
}
/* Ink level (rippling) */
.inkwell .ink::before {
  content: "";
  position: absolute;
  left: 4px; right: 4px;
  top: 50%;
  bottom: 8px;
  border-radius: 50%;
  background: #000;
  opacity: 0;
  transition: opacity 0.3s;
}

/* Nib dips into well when active */
.inkwell .nib {
  position: absolute;
  top: 2px;
  width: 8px;
  height: 18px;
  background: linear-gradient(180deg, #e8d8a8, #b08d3f);
  clip-path: polygon(30% 0, 70% 0, 100% 100%, 75% 88%, 60% 100%, 40% 100%, 25% 88%, 0 100%);
  transform: translateY(0);
  transition: transform 0.3s;
  z-index: 2;
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.4));
}
.inkwell .well-label {
  font-size: 10px;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  color: #e6d7a8;
  opacity: 0.6;
}

/* --- Active glow variants --- */
.inkwell.active .well-label { opacity: 1; color: #fff8e0; font-weight: 600; }
.inkwell.active .nib { transform: translateY(14px); }

.inkwell[data-tab="dashboard"].active .ink { background: radial-gradient(circle at 50% 30%, #3fbf9a, var(--emerald)); }
.inkwell[data-tab="invoices"].active .ink { background: radial-gradient(circle at 50% 30%, #f0c36a, var(--amber)); }
.inkwell[data-tab="add"].active .ink { background: radial-gradient(circle at 50% 30%, #ef7d6d, var(--crimson)); }
.inkwell[data-tab="search"].active .ink { background: radial-gradient(circle at 50% 30%, #6f8fd9, var(--cobalt)); }

.inkwell.active .ink { box-shadow: 0 0 18px 3px rgba(255,235,180,0.35), inset 0 -4px 8px rgba(0,0,0,0.4); }
.inkwell.active .ink::before { opacity: 0.45; background: rgba(0,0,0,0.35); }

/* ============ ACTION SHEET ============ */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20,12,8,0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 40;
}
.sheet {
  width: 100%;
  max-width: 480px;
  background: var(--secondary);
  border-radius: 16px 16px 0 0;
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  box-shadow: 0 -4px 20px rgba(0,0,0,0.4);
}
.sheet-title {
  text-align: center;
  font-family: "Segoe Script", cursive;
  font-size: 18px;
  color: #4a3a26;
  margin: 8px 0 14px;
}
.sheet-btn {
  display: block;
  width: 100%;
  padding: 16px;
  font-size: 16px;
  border: none;
  border-bottom: 1px solid #e2d5ba;
  background: transparent;
  font-family: inherit;
  cursor: pointer;
  color: var(--ink);
}
.sheet-btn:last-child { border-bottom: none; }
.sheet-danger { color: var(--red-wax); font-weight: 600; }
.sheet-cancel { color: #9a8a6a; }

/* ============ TOAST ============ */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%) translateY(20px);
  background: rgba(30,20,14,0.92);
  color: #f4efe6;
  padding: 10px 20px;
  border-radius: 30px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  z-index: 50;
  white-space: nowrap;
  box-shadow: 0 4px 14px rgba(0,0,0,0.4);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.hidden { display: none !important; }

@media (min-width: 720px) {
  .ledger { max-width: 640px; margin: 16px auto; }
  .sheet { max-width: 480px; margin: 0 auto; }
}