@import url('https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,500;9..144,600&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500;700&display=swap');

:root {
  --paper: #EFEAE0;
  --paper-raised: #F7F3EA;
  --ink: #22281F;
  --ink-dim: #5B6156;
  --rule: #D9D0BC;
  --pine: #2B4C3F;
  --pine-dim: #3E6754;
  --amber: #C98A2C;
  --berry: #8B3A46;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #1B1F1B;
    --paper-raised: #232922;
    --ink: #ECE7DA;
    --ink-dim: #A6AA9C;
    --rule: #363D34;
    --pine: #7FB79B;
    --pine-dim: #6FA98B;
    --amber: #D9A548;
    --berry: #D98A93;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: 'Inter', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

body { min-height: 100vh; }

.mono {
  font-family: 'JetBrains Mono', 'SF Mono', monospace;
  font-variant-numeric: tabular-nums;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
}

input, textarea {
  font-family: inherit;
  color: inherit;
}

::placeholder { color: var(--ink-dim); opacity: .8; }

/* ---------- Lock screen ---------- */

.lock-body {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.lock-card {
  width: 100%;
  max-width: 320px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.lock-mark {
  width: 56px;
  height: 56px;
  margin: 0 auto 8px;
  border-radius: 50%;
  background: var(--pine);
  color: var(--paper-raised);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 20px;
  letter-spacing: 0.02em;
}

.lock-card h1 {
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 26px;
  margin: 0;
}

.lock-sub {
  color: var(--ink-dim);
  font-size: 14px;
  margin: 0 0 6px;
}

.lock-card input {
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 14px 16px;
  font-size: 22px;
  text-align: center;
  letter-spacing: 0.3em;
}

.lock-card button {
  background: var(--pine);
  color: var(--paper-raised);
  border-radius: var(--radius);
  padding: 14px;
  font-size: 16px;
  font-weight: 600;
}

.lock-error {
  color: var(--berry);
  font-size: 13px;
  margin: 0;
}

/* ---------- App shell ---------- */

.app {
  max-width: 560px;
  margin: 0 auto;
  padding-bottom: calc(140px + var(--safe-bottom));
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--paper);
  padding: calc(16px + var(--safe-top)) 18px 14px;
  border-bottom: 1px solid var(--rule);
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

.brand {
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.02em;
  color: var(--ink-dim);
}

.logout-link {
  font-size: 12px;
  color: var(--ink-dim);
  text-decoration: none;
  background: none;
}

.date-nav {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
}

.date-nav button {
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: 10px;
  width: 34px;
  height: 34px;
  font-size: 16px;
  color: var(--ink);
  flex-shrink: 0;
}

.date-label {
  flex: 1;
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 19px;
  text-align: center;
}

.date-label small {
  display: block;
  font-family: 'Inter', sans-serif;
  font-weight: 400;
  font-size: 11px;
  color: var(--ink-dim);
  margin-top: 1px;
}

.date-input-hidden {
  position: absolute;
  opacity: 0;
  width: 34px;
  height: 34px;
  pointer-events: none;
}

/* Ledger total ticker */

.totals-card {
  margin: 14px 18px 0;
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 16px 18px;
}

.totals-main {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}

.totals-cal {
  font-size: 38px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.totals-cal-unit {
  font-size: 14px;
  color: var(--ink-dim);
  margin-left: 4px;
  font-weight: 400;
}

.totals-cal.tick {
  animation: tick 0.4s ease;
}
@keyframes tick {
  0% { transform: translateY(-4px); opacity: 0.4; }
  100% { transform: translateY(0); opacity: 1; }
}

.macro-row {
  display: flex;
  gap: 18px;
  margin-top: 12px;
}

.macro {
  flex: 1;
}

.macro-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-dim);
  margin-bottom: 4px;
}

.macro-val {
  font-size: 15px;
  font-weight: 500;
}

/* Ledger list */

.ledger {
  margin: 18px 18px 0;
}

.ledger-heading {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--ink-dim);
  margin: 0 2px 8px;
}

.entry-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 4px;
  border-bottom: 1px solid var(--rule);
}

.entry-time {
  font-size: 12px;
  color: var(--ink-dim);
  width: 44px;
  flex-shrink: 0;
}

.entry-main {
  flex: 1;
  min-width: 0;
}

.entry-desc {
  font-size: 15px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entry-macros {
  font-size: 11px;
  color: var(--ink-dim);
  margin-top: 2px;
}

.entry-cal {
  font-size: 15px;
  font-weight: 600;
  flex-shrink: 0;
}

.entry-row.expanded { flex-wrap: wrap; }

.entry-detail {
  width: 100%;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--rule);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.entry-detail-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.entry-detail-grid-dt {
  grid-template-columns: repeat(2, 1fr);
}

.entry-detail-grid label {
  display: block;
  font-size: 10px;
  text-transform: uppercase;
  color: var(--ink-dim);
  margin-bottom: 3px;
}

.entry-detail-grid input {
  width: 100%;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 8px;
  padding: 7px 8px;
  font-size: 14px;
}

.entry-note {
  font-size: 12px;
  color: var(--ink-dim);
  font-style: italic;
}

.entry-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.entry-actions button {
  font-size: 12px;
  padding: 7px 12px;
  border-radius: 8px;
  background: var(--paper);
  border: 1px solid var(--rule);
  color: var(--ink);
}

.entry-actions button.danger { color: var(--berry); }
.entry-actions button.primary { background: var(--pine); color: var(--paper-raised); border-color: var(--pine); }

.empty-state {
  text-align: center;
  padding: 30px 10px;
  color: var(--ink-dim);
  font-size: 14px;
}

/* Stamp animation on save */

.stamp {
  position: fixed;
  top: 46%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(-8deg) scale(0.7);
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 28px;
  letter-spacing: 0.08em;
  color: var(--amber);
  border: 3px solid var(--amber);
  border-radius: 10px;
  padding: 6px 18px;
  opacity: 0;
  pointer-events: none;
  z-index: 50;
  text-transform: uppercase;
}

.stamp.show {
  animation: stampIn 0.6s cubic-bezier(.2,1.4,.4,1) forwards;
}

@keyframes stampIn {
  0% { opacity: 0; transform: translate(-50%, -50%) rotate(-8deg) scale(1.6); }
  35% { opacity: 1; transform: translate(-50%, -50%) rotate(-8deg) scale(0.95); }
  75% { opacity: 1; transform: translate(-50%, -50%) rotate(-8deg) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -50%) rotate(-8deg) scale(1); }
}

/* ---------- Composer ---------- */

.composer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  background: linear-gradient(to top, var(--paper) 60%, transparent);
  padding: 18px 18px calc(18px + var(--safe-bottom));
}

.composer-inner {
  max-width: 560px;
  margin: 0 auto;
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: 18px;
  box-shadow: 0 8px 24px rgba(20, 24, 18, 0.12);
  padding: 12px 14px;
}

.composer textarea {
  width: 100%;
  border: none;
  background: transparent;
  resize: none;
  font-size: 15px;
  line-height: 1.4;
  min-height: 44px;
  max-height: 120px;
  outline: none;
}

.composer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 6px;
}

.composer-time {
  border: 1px solid var(--rule);
  background: var(--paper);
  border-radius: 9px;
  padding: 6px 8px;
  font-size: 12px;
  color: var(--ink-dim);
}

.log-btn {
  background: var(--pine);
  color: var(--paper-raised);
  border-radius: 12px;
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

.log-btn:disabled { opacity: 0.55; }

.spinner {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.4);
  border-top-color: #fff;
  animation: spin 0.7s linear infinite;
  display: none;
}
.log-btn.loading .spinner { display: inline-block; }
.log-btn.loading .log-btn-text { display: none; }

@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Confirm sheet ---------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 18, 0.45);
  z-index: 20;
  display: flex;
  align-items: flex-end;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.sheet-backdrop.show { opacity: 1; pointer-events: auto; }

.sheet {
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  background: var(--paper);
  border-radius: 20px 20px 0 0;
  padding: 20px 20px calc(20px + var(--safe-bottom));
  transform: translateY(100%);
  transition: transform 0.25s cubic-bezier(.2,.9,.3,1);
}
.sheet-backdrop.show .sheet { transform: translateY(0); }

.sheet h2 {
  font-family: 'Fraunces', serif;
  font-size: 19px;
  margin: 0 0 4px;
}

.sheet-source {
  font-size: 12px;
  color: var(--ink-dim);
  margin: 0 0 16px;
  font-style: italic;
}

.sheet-note {
  font-size: 12px;
  color: var(--amber);
  margin: 10px 0 0;
}

.sheet-actions {
  display: flex;
  gap: 10px;
  margin-top: 18px;
}

.sheet-actions button {
  flex: 1;
  padding: 13px;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
}

.sheet-actions .discard {
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  color: var(--ink);
}

.sheet-actions .save {
  background: var(--pine);
  color: var(--paper-raised);
}

.toast {
  position: fixed;
  bottom: calc(100px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--berry);
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  max-width: 80%;
  text-align: center;
}
.toast.show { opacity: 1; }
