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

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  color: #2a2620;
  background: #f7f2ea;
}

.loading {
  display: flex; align-items: center; justify-content: center;
  min-height: 60vh; color: #9a8d78;
}

.spinner {
  display: inline-block; width: 16px; height: 16px;
  border: 2px solid rgba(154, 141, 120, 0.3);
  border-top-color: #6b5f4e;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.spinner.small { width: 11px; height: 11px; border-width: 1.5px; }
@keyframes spin { to { transform: rotate(360deg); } }

.ledger-root {
  min-height: 100vh;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(176, 106, 59, 0.06), transparent 60%),
    radial-gradient(1000px 500px at 110% 10%, rgba(95, 122, 79, 0.06), transparent 55%),
    #f7f2ea;
  padding: 40px 24px 80px;
  padding-top: max(40px, env(safe-area-inset-top));
  padding-bottom: max(80px, env(safe-area-inset-bottom));
  position: relative;
}
.ledger-root::before {
  content: ""; position: absolute; inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' seed='3'/%3E%3CfeColorMatrix values='0 0 0 0 0.15 0 0 0 0 0.12 0 0 0 0 0.08 0 0 0 0.08 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  opacity: 0.5; pointer-events: none; mix-blend-mode: multiply;
}
.ledger-inner { max-width: 1240px; margin: 0 auto; position: relative; }

.ledger-header {
  display: flex; justify-content: space-between; align-items: flex-end;
  margin-bottom: 16px; padding-bottom: 24px;
  border-bottom: 1px solid rgba(42, 38, 32, 0.12);
  flex-wrap: wrap; gap: 16px;
}
.ledger-title {
  font-family: 'Fraunces', serif; font-weight: 400;
  font-size: clamp(42px, 6vw, 72px); line-height: 0.95;
  letter-spacing: -0.02em; margin: 0;
}
.ledger-title em { font-style: italic; font-weight: 300; color: #7a6b55; }

.ledger-meta { display: flex; gap: 20px; align-items: center; font-size: 13px; color: #6b5f4e; flex-wrap: wrap; }
.ledger-meta-stat { text-align: right; }
.ledger-meta-label { text-transform: uppercase; letter-spacing: 0.12em; font-size: 10px; color: #9a8d78; margin-bottom: 2px; }
.ledger-meta-value { font-family: 'Fraunces', serif; font-size: 22px; color: #2a2620; font-weight: 400; }
.ledger-meta-value.urgent { color: #b34a3a; }
.ledger-meta-value.today { color: #b08030; }
.ledger-save-indicator { display: inline-flex; align-items: center; gap: 6px; font-size: 11px; color: #9a8d78; text-transform: uppercase; letter-spacing: 0.1em; min-width: 60px; }

.toolbar { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin-bottom: 18px; }
.sort-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  font-family: inherit; font-size: 11px; color: #6b5f4e;
  background: #fffdf8; border: 1px solid rgba(42, 38, 32, 0.12);
  padding: 6px 12px; border-radius: 999px; cursor: pointer;
  letter-spacing: 0.06em; text-transform: uppercase;
  transition: all 0.15s ease;
}
.sort-toggle:hover { background: #fff; border-color: #b8ab94; color: #2a2620; }
.sort-toggle.active { background: #2a2620; color: #f7f2ea; border-color: #2a2620; }
.sort-toggle .icon { width: 11px; height: 11px; display: inline-flex; }
.sort-toggle .icon svg { width: 100%; height: 100%; }

.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 28px; }

.card {
  background: #fffdf8; border-radius: 4px; padding: 28px 28px 24px;
  box-shadow: 0 1px 0 rgba(42, 38, 32, 0.04), 0 4px 20px rgba(42, 38, 32, 0.06), 0 20px 40px -20px rgba(42, 38, 32, 0.08);
  border-top: 3px solid var(--accent); position: relative;
}
.cat-health { --accent: #5F7A4F; --accent-soft: #E8EEE0; --accent-text: #3E5133; }
.cat-home   { --accent: #B06A3B; --accent-soft: #F5E6D8; --accent-text: #7A4520; }
.cat-other  { --accent: #5D6890; --accent-soft: #E4E7F0; --accent-text: #3F476A; }

.card-header { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.card-icon {
  width: 36px; height: 36px; border-radius: 50%; background: var(--accent-soft);
  display: flex; align-items: center; justify-content: center; color: var(--accent-text);
}
.card-icon svg { width: 18px; height: 18px; }
.card-title { font-family: 'Fraunces', serif; font-size: 28px; font-weight: 500; margin: 0; letter-spacing: -0.01em; color: #2a2620; }
.card-count { margin-left: auto; font-size: 12px; color: #9a8d78; letter-spacing: 0.05em; font-variant-numeric: tabular-nums; }
.card-tagline { font-size: 13px; color: #7a6b55; font-style: italic; margin: 0 0 20px 0; padding-left: 48px; margin-top: -2px; }

.list { list-style: none; padding: 0; margin: 0 0 16px 0; }
.list-item {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 10px 0; border-bottom: 1px dashed rgba(42, 38, 32, 0.08);
  position: relative; animation: fadeIn 0.25s ease-out;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }
.list-item:last-child { border-bottom: none; }

.checkbox {
  flex-shrink: 0; width: 20px; height: 20px; border-radius: 50%;
  border: 1.5px solid #c9bda9; background: transparent; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  margin-top: 2px; transition: all 0.18s ease; padding: 0; color: white;
}
.checkbox:hover { border-color: var(--accent); background: var(--accent-soft); }
.checkbox.checked { background: var(--accent); border-color: var(--accent); }
.checkbox .icon-check svg { width: 13px; height: 13px; }

.item-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.item-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; position: relative; }

.item-text {
  font-size: 14.5px; line-height: 1.5; color: #2a2620;
  transition: color 0.2s ease; word-break: break-word; cursor: text;
  padding: 1px 4px; border-radius: 2px; flex: 1; min-width: 120px; margin-left: -4px;
}
.item-text:hover { background: rgba(201, 189, 169, 0.18); }
.item-text.done { color: #a89c87; text-decoration: line-through; text-decoration-color: #c9bda9; cursor: default; }
.item-text.done:hover { background: transparent; }

.edit-input {
  flex: 1; min-width: 120px; font-family: inherit; font-size: 14.5px;
  line-height: 1.5; color: #2a2620; background: #fff7e8;
  border: 1px solid var(--accent); border-radius: 3px; padding: 2px 6px; outline: none;
}

.due-pill, .add-date-btn {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; padding: 3px 8px 3px 7px; border-radius: 999px;
  cursor: pointer; transition: all 0.15s ease; font-family: inherit;
  font-weight: 500; letter-spacing: 0.01em; flex-shrink: 0;
}
.due-pill { background: #f0e9dd; color: #6b5f4e; border: 1px solid transparent; }
.due-pill:hover { background: #e6dccb; }
.due-pill.overdue { background: #fbe8e4; color: #b34a3a; border-color: #f0c8be; }
.due-pill.today { background: #fcf0d6; color: #946a1f; border-color: #f0dba7; }
.due-pill.soon { background: #fff7e0; color: #8a6b1a; }
.due-pill.upcoming { background: #ece7dc; color: #6b5f4e; }
.due-pill.done { opacity: 0.5; text-decoration: line-through; }
.due-pill .icon, .add-date-btn .icon { width: 10px; height: 10px; display: inline-flex; }
.due-pill .icon svg, .add-date-btn .icon svg { width: 100%; height: 100%; }
.due-pill .repeat-icon { width: 9px; height: 9px; margin-left: 2px; opacity: 0.85; }
.due-pill .repeat-icon svg { width: 100%; height: 100%; }

.add-date-btn {
  background: transparent; color: #b8ab94; border: 1px dashed #d3c6b0; opacity: 0;
}
.list-item:hover .add-date-btn, .add-date-btn:focus { opacity: 1; }
.add-date-btn:hover { color: var(--accent-text); border-color: var(--accent); background: var(--accent-soft); }

.date-popover {
  position: absolute; z-index: 10; right: 0; top: calc(100% + 4px);
  background: #fffdf8; border: 1px solid #d8cdb6; border-radius: 6px;
  padding: 10px 12px; box-shadow: 0 8px 24px rgba(42, 38, 32, 0.15);
  display: flex; flex-direction: column; gap: 8px; min-width: 240px;
}
.popover-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.date-popover input[type="date"], .date-popover input[type="number"], .date-popover select {
  font-family: inherit; font-size: 13px; padding: 4px 6px;
  border: 1px solid #d8cdb6; border-radius: 3px; background: white; color: #2a2620;
}
.date-popover input[type="number"] { width: 50px; }
.date-popover input[type="number"]:disabled { opacity: 0.5; background: #f5efe5; }
.popover-label { font-size: 11px; color: #7a6b55; text-transform: uppercase; letter-spacing: 0.06em; }
.date-popover button {
  background: none; border: none; cursor: pointer; font-family: inherit;
  font-size: 11px; padding: 4px 8px; border-radius: 3px;
  color: #6b5f4e; text-transform: uppercase; letter-spacing: 0.05em;
}
.date-popover button:hover { background: var(--accent-soft); color: var(--accent-text); }
.date-popover button.danger:hover { background: #fbe8e4; color: #b34a3a; }
.popover-actions { display: flex; justify-content: flex-end; gap: 4px; padding-top: 6px; border-top: 1px dashed rgba(42, 38, 32, 0.08); }

.item-actions { display: flex; gap: 2px; align-items: center; flex-shrink: 0; }
.icon-btn {
  opacity: 0; background: none; border: none; cursor: pointer;
  color: #b8ab94; padding: 4px; border-radius: 4px;
  display: flex; align-items: center;
  transition: opacity 0.15s ease, color 0.15s ease, background 0.15s ease;
}
.icon-btn svg { width: 14px; height: 14px; }
.list-item:hover .icon-btn, .icon-btn:focus { opacity: 1; }
.icon-btn:hover { color: var(--accent-text); background: var(--accent-soft); }
.icon-btn.delete:hover { color: #b34a3a; background: #fbe8e4; }

.add-row {
  display: flex; gap: 8px; align-items: center;
  padding: 8px 0 4px; border-top: 1px solid rgba(42, 38, 32, 0.08); margin-top: 4px;
}
.add-input {
  flex: 1; border: none; background: transparent; font-family: inherit;
  font-size: 14px; color: #2a2620; padding: 8px 0; outline: none;
}
.add-input::placeholder { color: #b8ab94; font-style: italic; }
.add-btn {
  background: var(--accent); color: white; border: none;
  width: 28px; height: 28px; border-radius: 50%; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.15s ease, opacity 0.15s ease;
}
.add-btn svg { width: 14px; height: 14px; }
.add-btn:hover { transform: scale(1.08); }

.card-footer { margin-top: 14px; padding-top: 10px; display: flex; justify-content: flex-end; }
.clear-btn {
  font-size: 11px; color: #9a8d78; background: none; border: none; cursor: pointer;
  letter-spacing: 0.08em; text-transform: uppercase; padding: 4px 8px; border-radius: 3px;
  transition: color 0.15s ease, background 0.15s ease;
}
.clear-btn:hover { color: var(--accent-text); background: var(--accent-soft); }

.empty-state { text-align: center; padding: 24px 0; color: #b8ab94; font-style: italic; font-size: 13px; }

@media (max-width: 640px) {
  .ledger-root { padding: 28px 16px 60px; }
  .card { padding: 22px 20px 20px; }
  .card-tagline { padding-left: 0; margin-top: 8px; }
  .icon-btn, .add-date-btn { opacity: 1; }
  .date-popover { right: auto; left: 0; min-width: 220px; }
}
