/* V0.12 layer: five-field entry, receipt flow, quick index, mobile calendar list, mobile finance cards/viewer,
   frozen direction bar and narrow-screen fixes. Same AI Manager palette (deep blue / light blue / coral). */
:root { --v12-sticky-top: calc(env(safe-area-inset-top, 0px) + 8px); --v12-nav-h: 72px; }

/* ---------- Five-field entry dialog ---------- */
.v12-entry { width: min(560px, calc(100vw - 24px)); padding: 0; border-radius: 20px; max-height: min(92dvh, 860px); overflow: hidden; }
.v12-entry form { display: flex; flex-direction: column; max-height: min(92dvh, 860px); }
.v12-entry-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 20px 22px 8px; }
.v12-entry-head h2 { margin: 2px 0 0; font-size: 20px; }
.v12-entry-head .eyebrow { margin: 0; font-size: 10px; }
.v12-icon-btn { width: 40px; height: 40px; border-radius: 50%; font-size: 22px; line-height: 1; color: var(--muted); background: var(--surface-2); flex-shrink: 0; }
.v12-entry-modes { margin: 4px 22px 4px; display: grid; grid-template-columns: 1fr 1fr; padding: 4px; gap: 4px; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--line); }
.v12-entry-modes button { min-height: 40px; border-radius: 9px; font-weight: 650; color: var(--muted); background: transparent; }
.v12-entry-modes button.active { background: var(--surface); color: var(--brand); box-shadow: 0 1px 3px #1F3A5F1F; }
.v12-entry-modes button:disabled:not(.active) { opacity: .45; cursor: not-allowed; }
#v12-entry-body { padding: 10px 22px 6px; overflow-y: auto; overscroll-behavior: contain; flex: 1; }
.v12-fields { display: grid; gap: 14px; }
.v12-field { display: grid; gap: 6px; margin: 0; }
.v12-label { font-size: 13px; font-weight: 650; color: var(--ink-2); }
.v12-label small { font-weight: 500; color: var(--muted-2); margin-left: 4px; }
.v12-field input, .v12-field select { min-height: 46px; font-size: 16px; border-radius: var(--r-ctl); }
.v12-amount-wrap { position: relative; display: block; }
.v12-amount-wrap input { width: 100%; font-size: 26px; font-weight: 700; padding-right: 72px; font-variant-numeric: tabular-nums; min-height: 56px; }
.v12-currency { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); padding: 4px 10px; border-radius: 999px; background: var(--brand-soft); color: var(--brand); font-size: 13px; font-weight: 700; }
.v12-field-error { color: var(--danger-ink); font-size: 12px; min-height: 0; }
.v12-field-error:empty { display: none; }
.v12-field [aria-invalid="true"] { border-color: var(--action); }
.v12-seg { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.v12-seg button { min-height: 46px; border-radius: var(--r-ctl); border: 1px solid var(--line); background: var(--surface); font-weight: 650; color: var(--muted); }
.v12-seg button.active[data-v12-direction="expense"] { background: var(--action-soft); border-color: var(--action); color: var(--action-ink); }
.v12-seg button.active[data-v12-direction="income"] { background: var(--ok-bg); border-color: var(--sky); color: var(--ok-ink); }
.v12-summary { margin: 14px 0 4px; padding: 10px 12px; border-radius: 12px; background: var(--surface-2); color: var(--ink-2); font-size: 13px; line-height: 1.55; }
.v12-summary:empty { display: none; }
.v12-receipt-link { display: flex; align-items: center; gap: 10px; width: 100%; margin: 0 0 14px; padding: 12px 14px; border-radius: 12px; border: 1px dashed var(--line-strong); background: var(--warm); color: var(--brand); font-weight: 650; text-align: left; min-height: 48px; }
.v12-receipt-link span { font-size: 20px; }
.v12-adv { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 6px; }
.v12-adv > summary { min-height: 44px; display: flex; align-items: center; font-weight: 650; color: var(--brand); cursor: pointer; }
.v12-adv fieldset { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px 12px; margin: 10px 0 0; }
.v12-adv legend { font-size: 13px; font-weight: 700; padding: 0 6px; color: var(--ink-2); }
.v12-adv .v12-risk { border-color: #F0C8BC; background: #FFF9F6; }
.v12-adv-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 12px; margin-top: 6px; }
.v12-adv-grid .span-all { grid-column: 1 / -1; }
.v12-adv label { font-size: 13px; }
.v12-adv .check { margin-top: 8px; }
.v12-entry-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 12px 22px calc(14px + env(safe-area-inset-bottom, 0px)); border-top: 1px solid var(--line); background: var(--surface); }
.v12-entry-foot button { min-height: 46px; padding: 0 18px; }
#v12-entry-error { margin: 0 22px; }
#v12-entry-error:empty { display: none; }
@media (max-width: 600px) {
  .v12-entry { width: 100vw; max-width: 100vw; max-height: 100dvh; margin: auto 0 0; border-radius: 20px 20px 0 0; }
  .v12-entry form { max-height: 100dvh; }
  .v12-entry-head, #v12-entry-body { padding-left: 16px; padding-right: 16px; }
  .v12-entry-modes { margin-inline: 16px; }
  .v12-entry-foot { padding-inline: 16px; }
  .v12-entry-foot button { flex: 1; }
  .v12-adv-grid { grid-template-columns: 1fr; }
}
.v12-quick-entries { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 4px; }
.v12-quick-entries button { min-height: 44px; }

/* ---------- Receipt capture ---------- */
.v12-receipt { width: min(560px, calc(100vw - 24px)); }
.v12-steps { list-style: none; counter-reset: s; margin: 6px 0 16px; padding: 0; display: grid; gap: 10px; }
.v12-steps li { counter-increment: s; display: grid; grid-template-columns: 32px 1fr; column-gap: 12px; align-items: start; }
.v12-steps li::before { content: counter(s); grid-row: span 2; width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: var(--brand); color: #fff; font-weight: 700; }
.v12-steps strong { font-size: 15px; color: var(--brand); }
.v12-steps span { font-size: 13px; color: var(--muted); line-height: 1.5; }
.v12-prompt blockquote { margin: 6px 0 12px; padding: 12px 14px; border-left: 3px solid var(--sky); background: var(--surface-2); border-radius: 0 10px 10px 0; font-size: 14px; line-height: 1.65; user-select: all; }
.v12-receipt-status { margin: 4px 0 14px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.v12-receipt-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.v12-receipt-actions button { min-height: 46px; }
.v12-receipt-more { margin-top: 14px; }
.v12-receipt-more summary { min-height: 44px; display: flex; align-items: center; color: var(--brand); font-weight: 600; }

/* ---------- Right-side quick index (同頁快速導覽) ---------- */
.v12-qi { position: fixed; right: 10px; top: 50%; transform: translateY(-50%); z-index: 25; opacity: 0; visibility: hidden; pointer-events: none;
  transition: opacity .25s ease, visibility 0s linear .25s; }
.v12-qi.v12-qi-on, .v12-qi.v12-qi-open, .v12-qi:focus-within { opacity: 1; visibility: visible; pointer-events: auto; transition: opacity .2s ease; }
.v12-qi ol { list-style: none; margin: 0; padding: 6px; display: grid; gap: 2px; border-radius: 14px; background: #FFFFFFD9; border: 1px solid #DFE6EFCC; box-shadow: 0 6px 18px #1F3A5F14; backdrop-filter: blur(6px); }
.v12-qi button[data-qi] { display: block; width: 100%; min-height: 32px; padding: 4px 10px; border-radius: 9px; font-size: 12px; color: var(--muted); text-align: right; white-space: nowrap; background: transparent; }
.v12-qi button[data-qi]:hover { background: var(--brand-soft); color: var(--brand); }
.v12-qi button[aria-current="true"] { color: var(--brand); font-weight: 700; background: var(--brand-soft); }
.v12-qi-toggle { display: none; }
body:has(dialog[open]) .v12-qi, body.v12-cal-full-open .v12-qi { display: none; }
@media (max-width: 900px) {
  .v12-qi { right: 6px; top: auto; bottom: calc(var(--v12-nav-h) + 24px + env(safe-area-inset-bottom, 0px)); transform: none; }
  .v12-qi-toggle { display: grid; place-items: center; width: 40px; height: 40px; margin-left: auto; border-radius: 50%; background: #FFFFFFE6; border: 1px solid var(--line); color: var(--brand); font-size: 18px; box-shadow: 0 4px 12px #1F3A5F1F; }
  .v12-qi ol { display: none; margin-bottom: 6px; }
  .v12-qi.v12-qi-open ol { display: grid; position: absolute; right: 0; bottom: 46px; }
  .v12-qi button[data-qi] { min-height: 40px; font-size: 13px; }
}
@media (prefers-reduced-motion: reduce) { .v12-qi { transition: none; } }

/* ---------- Mobile finance: frozen direction bar, preview cards, viewer ---------- */
.v12-sticky-direction { display: none; }
@media (max-width: 900px) {
  .v12-sticky-direction { display: block; position: fixed; left: 12px; right: 12px; top: var(--v12-sticky-top); z-index: 22; padding: 4px;
    border-radius: 14px; background: #FFFFFFF2; box-shadow: 0 6px 18px #1F3A5F1F; border: 1px solid var(--line);
    transform: translateY(-140%); opacity: 0; pointer-events: none; transition: transform .2s ease, opacity .2s ease; }
  .v12-sticky-direction.v12-on { transform: none; opacity: 1; pointer-events: auto; }
  .v12-sticky-direction .v11-direction { width: 100%; border: 0; background: transparent; padding: 0; }
  .v12-sticky-direction button { min-height: 40px; }
  body:has(dialog[open]) .v12-sticky-direction { display: none; }
  #finance-direction-bar { scroll-margin-top: 70px; }
  #view-finance [id], #view-finance .card { scroll-margin-top: 70px; }
}
@media (prefers-reduced-motion: reduce) { .v12-sticky-direction { transition: none; } }
.v12-chart-previews { margin-bottom: 22px; }
.v12-preview-list { display: grid; gap: 10px; margin: 6px 0 8px; }
.v12-preview { position: relative; display: grid; gap: 4px; width: 100%; padding: 14px 44px 14px 16px; border-radius: 14px; border: 1px solid var(--line); background: var(--surface); text-align: left; color: var(--ink); min-height: 88px; }
.v12-preview:active { background: var(--brand-soft); }
.v12-preview-title { font-size: 15px; font-weight: 700; color: var(--brand); }
.v12-preview-title small { font-size: 11px; font-weight: 600; color: var(--muted-2); margin-left: 4px; }
.v12-preview-purpose { font-size: 12px; color: var(--muted); line-height: 1.45; }
.v12-preview-value { font-size: 15px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.v12-preview-ctx { font-size: 11px; color: var(--muted-2); }
.v12-preview-go { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; background: var(--surface-2); color: var(--brand); }
.v12-viewer { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; padding: calc(12px + env(safe-area-inset-top, 0px)) 14px calc(14px + env(safe-area-inset-bottom, 0px)); border-radius: 0; overflow-y: auto; overscroll-behavior: contain; }
.v12-viewer-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; position: sticky; top: 0; z-index: 2; background: var(--surface); padding-bottom: 6px; }
.v12-viewer-direction { width: 100%; margin-bottom: 10px; }
.v12-viewer h2 { font-size: 18px; margin: 6px 0 2px; }
.v12-viewer-stage { touch-action: pan-y; min-height: 260px; }
.v12-viewer-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; }
.v12-viewer-foot button { min-width: 48px; min-height: 44px; }
.v12-dots { display: flex; gap: 6px; }
.v12-dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--line-strong); }
.v12-dots i.on { background: var(--brand); width: 18px; border-radius: 4px; }
.v12-chart-form ol { list-style: none; margin: 10px 0; padding: 0; display: grid; gap: 6px; }
.v12-chart-form li { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 12px; flex-wrap: wrap; }
.v12-chart-form li .check:first-child { flex: 1; min-width: 150px; }
.v12-chart-form li .row button { min-width: 40px; min-height: 40px; padding: 0; }
.v12-default { font-size: 12px; color: var(--muted); }

/* ---------- Mobile calendar list + full calendar ---------- */
#view-schedules.v12-cal-list .cal-scroll, #view-schedules.v12-cal-list .cal-month, #view-schedules.v12-cal-list .cal-legend,
#view-schedules.v12-cal-list .cal-record-list, #view-schedules.v12-cal-list #schedule-content > p.micro { display: none !important; }
#view-schedules.v12-cal-list #calendar-modes { display: none; }
.v12-cal-panel { padding: 16px; margin: 12px 0; }
.v12-cal-head { display: flex; flex-direction: column; gap: 10px; margin-bottom: 10px; }
.v12-cal-head strong { font-size: 16px; color: var(--brand); display: flex; gap: 8px; align-items: center; }
.v12-cal-seg button { min-height: 44px; font-size: 14px; }
.v12-cal-seg button[aria-selected="true"] { background: var(--brand); color: #fff; border-color: var(--brand); }
.v12-cal-seg span { font-weight: 500; font-size: 12px; opacity: .85; margin-left: 4px; }
.v12-cal-lists { touch-action: pan-y; }
.v12-cal-ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.v12-cal-item { display: grid; grid-template-columns: 92px minmax(0, 1fr) auto; gap: 10px; align-items: center; width: 100%; min-height: 56px; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); text-align: left; color: var(--ink); }
.v12-cal-item:active { background: var(--brand-soft); }
.v12-cal-time { font-variant-numeric: tabular-nums; font-size: 13px; font-weight: 650; color: var(--ink-2); display: grid; }
.v12-cal-time small { font-weight: 500; font-size: 11px; color: var(--muted-2); }
.v12-cal-main { display: grid; gap: 2px; min-width: 0; }
.v12-cal-main strong { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.v12-cal-main small { font-size: 12px; color: var(--muted); }
.v12-cal-free { border-style: dashed; background: #F7FBFF; }
.v12-cal-plus { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); font-weight: 700; }
.v12-cal-empty { padding: 18px 12px; border-radius: 12px; background: var(--surface-2); color: var(--muted); font-size: 14px; display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.v12-cal-empty:empty { display: none; }
.v12-cal-hint { margin: 10px 0; }
.v12-cal-expand { margin-top: 6px; min-height: 48px; }
#view-schedules.v12-cal-full { position: fixed; inset: 0; z-index: 70; background: var(--surface-2); overflow-y: auto; overscroll-behavior: contain; padding: calc(10px + env(safe-area-inset-top, 0px)) 12px calc(20px + env(safe-area-inset-bottom, 0px)); margin: 0; }
.v12-cal-fullbar { position: sticky; top: 0; z-index: 3; display: flex; align-items: center; gap: 12px; padding: 6px 0 10px; background: var(--surface-2); }
.v12-cal-fullbar strong { color: var(--brand); }
body.v12-cal-full-open .mobile-nav { display: none !important; }
@media (max-width: 600px) {
  #view-schedules.v12-cal-full .cal-month { gap: 3px; }
  #view-schedules.v12-cal-full .cal-month-event, #view-schedules.v12-cal-full .cal-month-day > button:not(.cal-date-hit) { display: none; }
  #view-schedules.v12-cal-full .cal-month-day { min-height: 64px; padding: 4px; }
  #view-schedules.v12-cal-full .cal-date-hit span { font-size: 10px; }
  #view-schedules.v12-cal-full .cal-date-hit .available-hours { display: none; }
}

/* ---------- Narrow-screen layout fixes (320–430px) ---------- */
@media (max-width: 900px) {
  /* Finance family tabs: one scrollable row instead of an irregular two-row wrap. */
  .section-tabs { flex-wrap: nowrap; overflow-x: auto; overscroll-behavior-x: contain; scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch; scrollbar-width: none; padding-bottom: 2px; margin-inline: -16px; padding-inline: 16px; }
  .section-tabs::-webkit-scrollbar { display: none; }
  .section-tabs button { flex: 0 0 auto; scroll-snap-align: start; white-space: nowrap; min-height: 42px; }
  .finance-heading { flex-wrap: nowrap; gap: 8px; }
  .finance-heading > .row { width: auto; flex-wrap: nowrap; gap: 6px; }
  .finance-heading > .row button { min-height: 42px; padding: 0 12px; white-space: nowrap; font-size: 13px; }
  .period-switch { gap: 2px; min-width: 0; }
  .period-switch .period-button { white-space: nowrap; font-size: 15px; padding: 0 6px; }
  .finance-stats { gap: 10px; }
  .finance-stats .stat-box { padding: 14px; min-width: 0; }
  .finance-stats .stat-box strong { font-size: clamp(20px, 6.2vw, 28px); overflow-wrap: anywhere; }
  .month-detail-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .month-detail-grid > * { min-width: 0; }
  .month-detail-grid strong { font-size: 15px; overflow-wrap: anywhere; }
  .month-controls strong { font-size: 14px; }
  #app main { padding-bottom: calc(var(--v12-nav-h) + 40px + env(safe-area-inset-bottom, 0px)); }
}
@media (max-width: 380px) {
  .finance-heading { flex-wrap: wrap; }
  .finance-heading > .row { width: 100%; }
  .finance-heading > .row button { flex: 1; }
  .finance-stats { grid-template-columns: 1fr 1fr; }
  .month-detail-grid { grid-template-columns: 1fr; }
  .v12-cal-item { grid-template-columns: 78px minmax(0, 1fr) auto; gap: 8px; padding: 10px; }
  .v11-direction { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
#v12-entry-form .v12-entry-modes { display: grid; grid-template-columns: 1fr 1fr; width: auto; align-self: stretch; justify-self: stretch; margin-left: 22px; margin-right: 22px; }
@media (max-width: 600px) { #v12-entry-form .v12-entry-modes { margin-left: 16px; margin-right: 16px; } }
#view-schedules.v12-cal-list .cal-toolbar [data-action="new-schedule"] { display: none; }
