/* Ledger styles, scoped to the Ledger tab so they don't collide with Ascent's
   classes (.modal, .overlay, .stat, .toast, …). */
@scope (.ledger-app) {
  :scope {

    /* structure, matched to the reference app */
    --paper: #14171c;
    --paper-2: #0f1114;
    --paper-raised: #1d2128;
    --surface-2: #262b33;
    --line: #333944;
    --line-soft: #282d35;
    --radius: 14px;
    --radius-sm: 9px;
    --shadow: 0 8px 24px rgba(0, 0, 0, .35);

    /* text + accent colors, unchanged from the ledger's own palette */
    --ink: #EDE6D3;
    --ink-soft: #ADB6A9;
    --ink-faint: #6E7A70;
    --forest: #4C9B76;
    --forest-deep: #7CC49E;
    --rust: #D97E6C;
    --rust-deep: #E8A392;
    --mustard: #D9A94A;
    --mustard-deep: #EFC876;
    --teal: #5AAFAE;
    --teal-deep: #86CBC9;
    --on-accent: #0d1a14;
  }

  * { box-sizing: border-box; }
  :scope {
    color: var(--ink);
    font-family: "Inter", system-ui, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
  }
  .mono { font-family: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace; font-variant-numeric: tabular-nums; }
  h1, h2, h3 { font-family: "Space Grotesk", "Inter", sans-serif; }

  a { color: inherit; }
  button { font-family: inherit; cursor: pointer; }
  input, select, textarea { font-family: inherit; }

  .wrap {
    max-width: 760px;
    margin: 0 auto;
    padding: 0.5rem 0 3rem;
  }

  /* ---------- Header ---------- */
  header.masthead {
    padding: 0.3rem 0 1.5rem;
    margin-bottom: 1.75rem;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
  }
  header.masthead h1 {
    margin: 0;
    font-size: 1.75rem;
    font-weight: 700;
    letter-spacing: 0.01em;
  }
  header.masthead .sub {
    margin: 0.2rem 0 0;
    color: var(--ink-faint);
    font-size: 0.85rem;
  }
  .head-actions {
    display: flex;
    gap: 0.5rem;
    align-items: center;
  }
  .icon-btn {
    background: none;
    border: 1px solid var(--line);
    color: var(--ink-soft);
    width: 2.2rem;
    height: 2.2rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    transition: border-color .15s ease, color .15s ease;
  }
  .icon-btn:hover { border-color: var(--forest); color: var(--forest); }

  /* ---------- Dashboard ---------- */
  .dashboard {
    display: flex;
    flex-wrap: wrap;
    gap: 1.3rem 1.75rem;
    background: linear-gradient(180deg, var(--paper-raised), var(--paper-2));
    border: 1px solid var(--line-soft);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.25rem 1.4rem;
    margin-bottom: 2.5rem;
  }
  .stat {
    text-align: left;
    min-width: 130px;
    flex: 1;
  }
  .stat .label {
    font-size: 0.68rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--ink-faint);
    margin: 0 0 0.5rem;
  }
  .stat .value {
    font-size: 1.5rem;
    font-weight: 600;
    margin: 0;
    line-height: 1.15;
  }
  .stat.cash .value { color: var(--forest-deep); }
  .stat.owed .value { color: var(--rust-deep); }
  .stat.repaid .value { color: var(--teal-deep); }

  /* ---------- Section headers ---------- */
  .section-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.9rem;
  }
  .section-head h2 {
    font-size: 1.05rem;
    font-weight: 600;
    margin: 0;
  }
  .btn {
    border: 1px solid transparent;
    background: var(--forest);
    color: var(--on-accent);
    padding: 0.55rem 1.05rem;
    font-size: 0.85rem;
    font-weight: 600;
    border-radius: 999px;
    transition: opacity .15s ease, transform .15s ease;
  }
  .btn:hover { opacity: 0.88; }
  .btn.ghost {
    background: transparent;
    border-color: var(--line);
    color: var(--ink-soft);
  }
  .btn.ghost:hover { border-color: var(--forest); color: var(--forest); opacity: 1; }
  .btn.danger {
    background: var(--rust);
    color: var(--on-accent);
  }
  .btn.small {
    padding: 0.34rem 0.8rem;
    font-size: 0.78rem;
  }
  .btn:disabled { opacity: 0.4; cursor: default; }

  /* ---------- Ledger list ---------- */
  .ledger-list { display: flex; flex-direction: column; gap: 0.6rem; margin-bottom: 2.5rem; }
  .ledger-row {
    background: var(--paper-raised);
    border: 1px solid var(--line-soft);
    border-radius: var(--radius-sm);
    padding: 0.9rem 1.1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    cursor: pointer;
    transition: border-color .15s ease, transform .1s ease;
  }
  .ledger-row:hover { border-color: var(--line); transform: translateY(-1px); }
  .ledger-row .info { min-width: 0; }
  .ledger-row .name {
    font-weight: 600;
    font-size: 1.0rem;
    margin: 0 0 0.2rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .ledger-row .meta {
    margin: 0;
    font-size: 0.76rem;
    color: var(--ink-faint);
  }
  .ledger-row .net {
    font-size: 1.0rem;
    font-weight: 600;
    white-space: nowrap;
  }
  .ledger-row .net.pos { color: var(--forest-deep); }
  .ledger-row .net.neg { color: var(--rust-deep); }
  .ledger-row .row-actions {
    display: flex;
    gap: 0.35rem;
    flex-shrink: 0;
  }

  .empty {
    border: 1px dashed var(--line);
    border-radius: var(--radius-sm);
    padding: 2rem 1.25rem;
    text-align: center;
    color: var(--ink-faint);
    font-size: 0.92rem;
  }
  .empty p { margin: 0 0 0.9rem; }

  /* ---------- Ledger detail ---------- */
  .back-link {
    background: none;
    border: none;
    color: var(--ink-soft);
    font-size: 0.85rem;
    padding: 0;
    margin-bottom: 1.1rem;
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
  }
  .back-link:hover { color: var(--forest); }

  .ledger-title-row {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.3rem;
  }
  .ledger-title-row h2 {
    margin: 0;
    font-size: 1.45rem;
    font-weight: 700;
  }
  .ledger-title-actions { display: flex; gap: 0.4rem; flex-shrink: 0; }

  .ledger-summary {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.9rem;
    background: var(--paper-raised);
    border: 1px solid var(--line-soft);
    border-radius: var(--radius);
    padding: 1rem 1.1rem;
    margin: 1.2rem 0 2rem;
  }
  .ledger-summary .s { padding: 0; }
  .ledger-summary .s .l { font-size: 0.64rem; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-faint); margin: 0 0 0.35rem; }
  .ledger-summary .s .v { font-size: 1.02rem; font-weight: 600; margin: 0; }
  @media (max-width: 520px) {
    .ledger-summary { grid-template-columns: repeat(2, 1fr); }
  }

  .entry-form {
    background: var(--paper-raised);
    border: 1px solid var(--line-soft);
    border-radius: var(--radius);
    padding: 1.2rem;
    margin-bottom: 2rem;
  }
  .entry-form .grid, .modal .grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
  }
  .field { display: flex; flex-direction: column; gap: 0.35rem; }
  .field label { font-size: 0.72rem; color: var(--ink-faint); }
  .field input, .field select {
    background: var(--paper-2);
    border: 1px solid var(--line);
    color: var(--ink);
    padding: 0.55rem 0.65rem;
    font-size: 0.92rem;
    border-radius: var(--radius-sm);
  }
  .field input:focus, .field select:focus {
    outline: 2px solid var(--forest);
    outline-offset: 1px;
  }
  .type-toggle {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 3px;
    background: var(--paper-2);
    border-radius: 999px;
    padding: 3px;
    margin-bottom: 0.85rem;
  }
  .type-btn {
    border: none;
    background: transparent;
    color: var(--ink-faint);
    padding: 0.5rem 0.3rem;
    font-size: 0.78rem;
    font-weight: 500;
    border-radius: 999px;
    text-align: center;
    transition: background .15s ease, color .15s ease;
  }
  .type-btn[aria-pressed="true"] { color: var(--on-accent); }
  .type-btn.income[aria-pressed="true"] { background: var(--forest); }
  .type-btn.expense[aria-pressed="true"] { background: var(--rust); }
  .type-btn.debt[aria-pressed="true"] { background: var(--mustard); }
  .type-btn.repaid[aria-pressed="true"] { background: var(--teal); }

  .entry-list { display: flex; flex-direction: column; gap: 0.5rem; }
  .entry-row {
    display: grid;
    grid-template-columns: auto 1fr auto auto;
    align-items: center;
    gap: 0.7rem;
    padding: 0.75rem 0.9rem;
    background: var(--paper-raised);
    border: 1px solid var(--line-soft);
    border-radius: var(--radius-sm);
  }
  .entry-row .badge {
    font-size: 0.65rem;
    font-weight: 600;
    padding: 0.25rem 0.55rem;
    border-radius: 999px;
    color: var(--on-accent);
    white-space: nowrap;
  }
  .badge.income { background: var(--forest); }
  .badge.expense { background: var(--rust); }
  .badge.debt { background: var(--mustard); }
  .badge.repaid { background: var(--teal); }
  .entry-row .desc { min-width: 0; }
  .entry-row .desc .d { margin: 0; font-size: 0.92rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .entry-row .desc .date { margin: 0.15rem 0 0; font-size: 0.72rem; color: var(--ink-faint); }
  .entry-row .amt { font-weight: 600; white-space: nowrap; }
  .entry-row .amt.income, .entry-row .amt.debt { color: var(--forest-deep); }
  .entry-row .amt.expense, .entry-row .amt.repaid { color: var(--rust-deep); }
  .entry-row .row-actions { display: flex; gap: 0.3rem; }
  .row-actions button {
    background: none;
    border: none;
    color: var(--ink-faint);
    font-size: 0.85rem;
    padding: 0.2rem 0.3rem;
  }
  .row-actions button:hover { color: var(--ink); }

  /* ---------- Modal ---------- */
  .overlay {
    position: fixed;
    inset: 0;
    background: rgba(6, 8, 7, 0.6);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    z-index: 50;
    padding: env(safe-area-inset-bottom, 0px) 0 0;
  }
  @media (min-width: 560px) {
    .overlay { align-items: center; }
  }
  .modal {
    background: var(--paper-raised);
    border: 1px solid var(--line-soft);
    border-bottom: none;
    width: 100%;
    max-width: 420px;
    padding: 1.4rem 1.3rem calc(1.4rem + env(safe-area-inset-bottom, 0px));
    box-shadow: var(--shadow);
  }
  @media (min-width: 560px) {
    .modal { border-bottom: 1px solid var(--line-soft); border-radius: var(--radius); }
  }
  .modal h3 { margin: 0 0 1rem; font-size: 1.1rem; font-weight: 600; }
  .modal p.note { color: var(--ink-faint); font-size: 0.86rem; margin: -0.4rem 0 1rem; }
  .modal .field { margin-bottom: 0.85rem; }
  .modal .actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1.1rem; }

  .toast {
    position: fixed;
    bottom: calc(1.2rem + env(safe-area-inset-bottom, 0px));
    left: 50%;
    transform: translateX(-50%);
    background: var(--surface-2);
    border: 1px solid var(--line);
    color: var(--ink);
    padding: 0.6rem 1.1rem;
    border-radius: 999px;
    font-size: 0.85rem;
    z-index: 60;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease, transform .2s ease;
  }
  .toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

  .sync-note {
    font-size: 0.72rem;
    color: var(--ink-faint);
    text-align: center;
    margin-top: 2.5rem;
  }

  ::selection { background: var(--forest); color: var(--on-accent); }
  :focus-visible { outline: 2px solid var(--forest); outline-offset: 2px; }
}
