/* ---------------------------------------------------------------------------
   Receipt Vault — visual system
   Palette : warm receipt paper + deep slate-teal ink + pine / amber / brick
   Type    : Space Grotesk (display) · Inter (body) · Space Mono (data)
   Signature: receipt cards with a perforated top edge + a rotated warranty stamp
---------------------------------------------------------------------------- */
:root {
  --paper:      #f4f0e6;
  --paper-2:    #ece6d7;
  --surface:    #fdfbf5;
  --ink:        #14302c;
  --ink-soft:   #51635b;
  --ink-faint:  #8a9690;
  --pine:       #1c6b5b;
  --pine-deep:  #134c40;
  --amber:      #b9791a;
  --brick:      #a9453e;
  --hairline:   #ddd6c5;
  --hairline-2: #cfc7b3;

  --r-sm: 6px;
  --r-md: 12px;
  --r-lg: 18px;
  --shadow: 0 1px 2px rgba(20,48,44,.06), 0 8px 24px rgba(20,48,44,.07);
  --shadow-lg: 0 12px 40px rgba(20,48,44,.18);

  --font-display: "Space Grotesk", system-ui, sans-serif;
  --font-body: "Inter", system-ui, -apple-system, sans-serif;
  --font-mono: "Space Mono", ui-monospace, "SF Mono", Menlo, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

body {
  background-image:
    radial-gradient(circle at 1px 1px, rgba(20,48,44,.05) 1px, transparent 0);
  background-size: 22px 22px;
  min-height: 100vh;
}

.hidden { display: none !important; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0);
}

button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; font-size: 1rem; }

/* --- Buttons -------------------------------------------------------------- */
.btn {
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  padding: .6rem 1rem;
  font-weight: 600;
  font-size: .92rem;
  letter-spacing: .01em;
  transition: transform .08s ease, background .15s ease, border-color .15s ease;
}
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--pine); outline-offset: 2px; }

.btn-primary { background: var(--pine); color: #fff; }
.btn-primary:hover { background: var(--pine-deep); }
.btn-ghost { background: transparent; color: var(--ink); border-color: var(--hairline-2); }
.btn-ghost:hover { background: var(--paper-2); }
.btn-danger { background: transparent; color: var(--brick); border-color: rgba(169,69,62,.4); }
.btn-danger:hover { background: rgba(169,69,62,.08); }
.plus { font-size: 1.1em; line-height: 0; }

.icon-btn {
  background: transparent; border: none; color: var(--ink-soft);
  font-size: 1.1rem; width: 2.2rem; height: 2.2rem; border-radius: var(--r-sm);
  display: grid; place-items: center;
}
.icon-btn:hover { background: var(--paper-2); color: var(--ink); }

/* --- Lock screen ---------------------------------------------------------- */
.lock {
  position: fixed; inset: 0; display: grid; place-items: center;
  padding: 1.5rem; background: var(--paper);
}
.lock-card {
  background: var(--surface); border: 1px solid var(--hairline);
  border-radius: var(--r-lg); padding: 2.5rem 2rem; width: min(380px, 100%);
  text-align: center; box-shadow: var(--shadow); display: grid; gap: .85rem;
}
.lock-mark { font-size: 2.4rem; color: var(--pine); }
.lock-card h1 { font-family: var(--font-display); margin: 0; font-size: 1.5rem; }
.lock-card p { margin: 0; color: var(--ink-soft); font-size: .92rem; }
.lock-card input {
  padding: .75rem .9rem; border: 1px solid var(--hairline-2); border-radius: var(--r-sm);
  background: var(--paper); text-align: center;
}
.lock-card input:focus { outline: 2px solid var(--pine); outline-offset: 1px; border-color: transparent; }
.lock-error { color: var(--brick); min-height: 1.2em; font-size: .85rem; }

/* --- Top bar -------------------------------------------------------------- */
.app { max-width: 1100px; margin: 0 auto; padding: 0 1.1rem 5rem; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 0 .85rem; margin-bottom: .5rem;
  background: linear-gradient(var(--paper) 78%, transparent);
  backdrop-filter: blur(2px);
}
.brand { display: flex; align-items: center; gap: .55rem; }
.brand-mark {
  font-size: 1.5rem; color: var(--pine);
  background: var(--surface); border: 1px solid var(--hairline);
  width: 2.2rem; height: 2.2rem; display: grid; place-items: center; border-radius: 8px;
}
.brand-name { font-family: var(--font-display); font-weight: 700; font-size: 1.25rem; letter-spacing: -.01em; }
.brand-version {
  font-family: var(--font-mono); font-size: .68rem; color: var(--ink-faint);
  background: var(--surface); border: 1px solid var(--hairline);
  border-radius: 100px; padding: .1rem .45rem; align-self: center; letter-spacing: .02em;
}
.lock-version { font-family: var(--font-mono); font-size: .7rem; color: var(--ink-faint); }
.topbar-actions { display: flex; gap: .5rem; align-items: center; }

/* Add-receipt menu */
.add-group { position: relative; }
.add-menu {
  position: absolute; top: calc(100% + .4rem); right: 0; z-index: 30;
  background: var(--surface); border: 1px solid var(--hairline); border-radius: var(--r-md);
  box-shadow: var(--shadow-lg); padding: .35rem; min-width: 220px; display: grid; gap: .15rem;
  animation: menuin .12s ease;
}
@keyframes menuin { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
.add-menu-item {
  display: flex; align-items: center; gap: .6rem; width: 100%; text-align: left;
  background: transparent; border: none; border-radius: var(--r-sm);
  padding: .6rem .65rem; font-size: .92rem; color: var(--ink); font-weight: 500;
}
.add-menu-item:hover, .add-menu-item:focus-visible { background: var(--paper-2); outline: none; }
.add-menu-icon { color: var(--pine); font-size: 1.05rem; width: 1.2rem; text-align: center; }
.add-menu-sub { margin-left: auto; font-family: var(--font-mono); font-size: .68rem; color: var(--ink-faint); }

/* --- Stats ---------------------------------------------------------------- */
.stats {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: .7rem; margin-bottom: 1.2rem;
}
.stat {
  background: var(--surface); border: 1px solid var(--hairline);
  border-radius: var(--r-md); padding: .9rem 1rem; display: grid; gap: .15rem;
}
.stat-num {
  font-family: var(--font-display); font-weight: 700; font-size: 1.5rem; color: var(--ink);
  font-variant-numeric: tabular-nums;
}
.stat-label { font-size: .72rem; text-transform: uppercase; letter-spacing: .07em; color: var(--ink-faint); }
.stat-alert.is-on { border-color: rgba(185,121,26,.5); background: rgba(185,121,26,.06); }
.stat-alert.is-on .stat-num { color: var(--amber); }

/* --- Controls ------------------------------------------------------------- */
.controls { display: grid; gap: .7rem; margin-bottom: 1.4rem; }
.search-wrap { position: relative; }
.search-icon {
  position: absolute; left: .85rem; top: 50%; transform: translateY(-50%);
  color: var(--ink-faint); font-size: 1.2rem;
}
#search {
  width: 100%; padding: .8rem .9rem .8rem 2.4rem;
  border: 1px solid var(--hairline-2); border-radius: var(--r-sm);
  background: var(--surface);
}
#search:focus { outline: 2px solid var(--pine); outline-offset: 1px; border-color: transparent; }
.filters { display: flex; gap: .5rem; flex-wrap: wrap; }
.filters select {
  flex: 1; min-width: 9rem; padding: .6rem .7rem;
  border: 1px solid var(--hairline-2); border-radius: var(--r-sm);
  background: var(--surface); color: var(--ink);
}

/* --- Receipt grid + cards (signature element) ----------------------------- */
.grid {
  display: grid; gap: 1.1rem;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
}
.card {
  position: relative; background: var(--surface);
  border: 1px solid var(--hairline); border-top: none;
  border-radius: 0 0 var(--r-md) var(--r-md);
  padding: 1rem 1rem 1.1rem; cursor: pointer; overflow: hidden;
  box-shadow: var(--shadow);
  transition: transform .12s ease, box-shadow .12s ease;
}
.card:hover { transform: translateY(-3px); box-shadow: var(--shadow-lg); }
.card:focus-visible { outline: 2px solid var(--pine); outline-offset: 2px; }
/* perforated, torn-receipt top edge */
.card::before {
  content: ""; position: absolute; left: 0; right: 0; top: 0; height: 7px;
  background:
    radial-gradient(circle at 5px 8px, transparent 4px, var(--surface) 4px) repeat-x;
  background-size: 12px 12px; background-position: 0 -4px;
  border-top: 1px solid var(--hairline);
}

.card-thumb {
  width: 100%; height: 130px; object-fit: cover; border-radius: var(--r-sm);
  background: var(--paper-2); margin: .3rem 0 .8rem; display: block;
}
.card-thumb.placeholder { display: grid; place-items: center; color: var(--ink-faint); font-size: 1.8rem; }

.card-merchant {
  font-family: var(--font-display); font-weight: 600; font-size: 1.05rem;
  margin: 0 0 .15rem; line-height: 1.25;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.card-meta { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; margin-top: .35rem; }
.card-date { font-family: var(--font-mono); font-size: .8rem; color: var(--ink-soft); }
.card-amount { font-family: var(--font-mono); font-weight: 700; font-size: 1rem; color: var(--ink); }
.card-cat {
  display: inline-block; margin-top: .55rem; font-size: .7rem; letter-spacing: .04em;
  text-transform: uppercase; color: var(--ink-soft);
  background: var(--paper-2); border-radius: 100px; padding: .2rem .55rem;
}

/* rotated warranty "stamp" */
.stamp {
  position: absolute; top: 16px; right: -2px; transform: rotate(4deg);
  font-family: var(--font-mono); font-size: .62rem; font-weight: 700;
  letter-spacing: .08em; text-transform: uppercase;
  padding: .22rem .5rem; border: 1.5px solid; border-radius: 3px;
  background: var(--surface);
}
.stamp.active   { color: var(--pine);  border-color: var(--pine); }
.stamp.expiring { color: var(--amber); border-color: var(--amber); }
.stamp.expired  { color: var(--brick); border-color: var(--brick); opacity: .8; }

/* --- Empty state ---------------------------------------------------------- */
.empty {
  text-align: center; padding: 4rem 1rem; color: var(--ink-soft);
  display: grid; gap: .5rem; place-items: center;
}
.empty-mark { font-size: 3rem; color: var(--hairline-2); }
.empty h2 { font-family: var(--font-display); margin: .3rem 0 0; color: var(--ink); }
.empty p { max-width: 26rem; margin: 0 0 .8rem; }

/* --- Drawer (editor) ------------------------------------------------------ */
.drawer { position: fixed; inset: 0; z-index: 40; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(20,48,44,.45); backdrop-filter: blur(2px); }
.drawer-panel {
  position: absolute; top: 0; right: 0; height: 100%; width: min(480px, 100%);
  background: var(--surface); box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column; animation: slidein .22s ease;
}
@keyframes slidein { from { transform: translateX(20px); opacity: .6; } to { transform: none; opacity: 1; } }

.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1.1rem 1.2rem; border-bottom: 1px solid var(--hairline);
}
.drawer-head h2 { font-family: var(--font-display); margin: 0; font-size: 1.2rem; }
.drawer-body { padding: 1.2rem; overflow-y: auto; flex: 1; }
.drawer-foot {
  display: flex; justify-content: space-between; align-items: center; gap: .6rem;
  padding: .9rem 1.2rem; border-top: 1px solid var(--hairline); background: var(--surface);
}
.foot-right { display: flex; gap: .5rem; }

.banner {
  margin: 0 0 1rem; padding: .65rem .8rem; border-radius: var(--r-sm);
  font-size: .85rem; background: rgba(185,121,26,.1); color: var(--amber);
  border: 1px solid rgba(185,121,26,.3);
}

.receipt-image {
  width: 100%; padding: 0; border: 1px solid var(--hairline); background: var(--paper-2);
  border-radius: var(--r-md); overflow: hidden; margin-bottom: 1.2rem; cursor: zoom-in; display: block;
}
.receipt-image img { width: 100%; max-height: 260px; object-fit: contain; display: block; }
.open-original {
  display: inline-flex; align-items: center; gap: .35rem; margin: -.6rem 0 1.1rem;
  font-size: .8rem; font-weight: 600; color: var(--pine); text-decoration: none;
}
.open-original::before { content: "⤓"; font-size: .95rem; }
.open-original:hover { text-decoration: underline; }

.card-type {
  position: absolute; top: 14px; left: 14px;
  font-family: var(--font-mono); font-size: .58rem; font-weight: 700; letter-spacing: .1em;
  color: var(--ink-soft); background: var(--surface);
  border: 1px solid var(--hairline); border-radius: 3px; padding: .12rem .35rem;
}

.form { display: grid; gap: 1rem; }
.field { display: grid; gap: .35rem; }
.field > span { font-size: .78rem; font-weight: 600; letter-spacing: .03em; text-transform: uppercase; color: var(--ink-soft); }
.field input, .field textarea, .field select {
  padding: .65rem .75rem; border: 1px solid var(--hairline-2); border-radius: var(--r-sm);
  background: var(--paper); color: var(--ink); width: 100%;
}
.field input:focus, .field textarea:focus {
  outline: 2px solid var(--pine); outline-offset: 1px; border-color: transparent; background: var(--surface);
}
.field textarea { resize: vertical; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.field-amount .amount-input { display: flex; gap: .4rem; }
.amount-input .currency { width: 4.2rem; text-align: center; text-transform: uppercase; font-family: var(--font-mono); }
.amount-input input[name="total_amount"] { font-family: var(--font-mono); flex: 1; }
.hint { font-size: .78rem; color: var(--ink-faint); font-family: var(--font-mono); min-height: 1em; }

.extracted { border-top: 1px solid var(--hairline); padding-top: .8rem; }
.extracted summary { cursor: pointer; font-size: .82rem; font-weight: 600; color: var(--ink-soft); }
.items-list { margin: .7rem 0; display: grid; gap: .3rem; }
.item-row { display: flex; justify-content: space-between; font-family: var(--font-mono); font-size: .8rem; gap: 1rem; }
.item-row span:last-child { color: var(--ink-soft); }
.raw-text {
  white-space: pre-wrap; word-break: break-word; font-family: var(--font-mono); font-size: .72rem;
  color: var(--ink-soft); background: var(--paper); border: 1px solid var(--hairline);
  border-radius: var(--r-sm); padding: .7rem; max-height: 180px; overflow: auto; margin: .6rem 0 0;
}

/* --- Lightbox ------------------------------------------------------------- */
.lightbox {
  position: fixed; inset: 0; z-index: 60; background: rgba(12,22,20,.92);
  display: grid; place-items: center; padding: 1.5rem;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: var(--r-sm); }
.lightbox-close { position: absolute; top: 1rem; right: 1rem; color: #fff; font-size: 1.4rem; }
.lightbox-close:hover { background: rgba(255,255,255,.15); color: #fff; }

/* --- Busy / scanner ------------------------------------------------------- */
.busy { position: fixed; inset: 0; z-index: 70; display: grid; place-items: center; background: rgba(20,48,44,.5); backdrop-filter: blur(3px); }
.busy-card { background: var(--surface); border-radius: var(--r-lg); padding: 2rem 2.4rem; text-align: center; box-shadow: var(--shadow-lg); }
.busy-card p { margin: 1.1rem 0 0; font-family: var(--font-mono); font-size: .85rem; color: var(--ink-soft); }
.scanner {
  width: 64px; height: 80px; margin: 0 auto; border: 2px solid var(--pine);
  border-radius: 4px; position: relative; overflow: hidden; background: var(--paper);
}
.scanner span {
  position: absolute; left: 0; right: 0; height: 3px; background: var(--pine);
  box-shadow: 0 0 8px var(--pine); animation: scan 1.3s ease-in-out infinite;
}
@keyframes scan { 0%,100% { top: 6px; } 50% { top: 70px; } }

/* --- Toast ---------------------------------------------------------------- */
.toast {
  position: fixed; left: 50%; bottom: 1.4rem; transform: translateX(-50%);
  z-index: 80; background: var(--ink); color: var(--paper);
  padding: .7rem 1.1rem; border-radius: var(--r-sm); font-size: .88rem; box-shadow: var(--shadow-lg);
  animation: toastin .2s ease;
}
.toast.err { background: var(--brick); }
@keyframes toastin { from { transform: translate(-50%, 8px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }

/* --- Responsive ----------------------------------------------------------- */
@media (max-width: 720px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .filters select { min-width: calc(50% - .25rem); }
  .drawer-panel { width: 100%; top: auto; bottom: 0; height: 92%; border-radius: var(--r-lg) var(--r-lg) 0 0; animation: slideup .24s ease; }
  @keyframes slideup { from { transform: translateY(24px); } to { transform: none; } }
  .field-row { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* --- Auth screen (login / register) -------------------------------------- */
.auth { position: fixed; inset: 0; display: grid; place-items: center; padding: 1.5rem; background: var(--paper); }
.auth-card {
  background: var(--surface); border: 1px solid var(--hairline); border-radius: var(--r-lg);
  padding: 2.2rem 2rem; width: min(400px, 100%); box-shadow: var(--shadow);
}
.auth-brand { text-align: center; margin-bottom: 1.4rem; }
.auth-mark { font-size: 2.4rem; color: var(--pine); display: block; }
.auth-brand h1 { font-family: var(--font-display); margin: .4rem 0 .2rem; font-size: 1.5rem; }
.auth-tag { margin: 0; color: var(--ink-soft); font-size: .9rem; }
.auth-tabs { display: flex; background: var(--paper-2); border-radius: var(--r-sm); padding: 4px; margin-bottom: 1.3rem; }
.auth-tab {
  flex: 1; background: none; border: none; padding: .55rem; border-radius: calc(var(--r-sm) - 2px);
  font-weight: 600; font-size: .9rem; color: var(--ink-soft);
}
.auth-tab.is-active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.auth-form { display: grid; gap: 1rem; }
.auth-submit { width: 100%; padding: .75rem; margin-top: .2rem; }
.auth-error { color: var(--brick); min-height: 1.2em; font-size: .85rem; margin: 0; text-align: center; }
.auth-version { display: block; text-align: center; margin-top: 1.2rem; font-family: var(--font-mono); font-size: .7rem; color: var(--ink-faint); }

/* --- User chip + menu ----------------------------------------------------- */
.user-group { position: relative; }
.user-btn { display: flex; align-items: center; gap: .5rem; padding: .45rem .7rem .45rem .5rem; }
.user-avatar {
  width: 1.7rem; height: 1.7rem; border-radius: 50%; background: var(--pine); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: .8rem; text-transform: uppercase;
}
.user-name { font-weight: 600; font-size: .9rem; max-width: 9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-menu { left: auto; right: 0; min-width: 190px; }

/* --- Admin panel ---------------------------------------------------------- */
.admin-section { margin-bottom: 1.8rem; }
.admin-section h3 { font-family: var(--font-display); font-size: 1rem; margin: 0 0 .3rem; }
.admin-hint { color: var(--ink-soft); font-size: .82rem; margin: 0 0 .8rem; }
.admin-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.btn-sm { padding: .4rem .7rem; font-size: .82rem; }
.seg { display: flex; background: var(--paper-2); border-radius: var(--r-sm); padding: 4px; }
.seg-btn {
  flex: 1; background: none; border: none; padding: .5rem; border-radius: calc(var(--r-sm) - 2px);
  font-weight: 600; font-size: .88rem; color: var(--ink-soft);
}
.seg-btn.is-active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.invite-list { display: grid; gap: .5rem; margin-top: .9rem; }
.invite-item {
  display: flex; align-items: center; justify-content: space-between; gap: .6rem;
  background: var(--paper); border: 1px solid var(--hairline); border-radius: var(--r-sm); padding: .55rem .7rem;
}
.invite-code { font-family: var(--font-mono); font-size: .85rem; word-break: break-all; }
.invite-code.used { text-decoration: line-through; color: var(--ink-faint); }
.invite-status { font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-faint); white-space: nowrap; }
.invite-status.open { color: var(--pine); }
.invite-copy { background: none; border: 1px solid var(--hairline-2); border-radius: var(--r-sm); padding: .3rem .55rem; font-size: .75rem; color: var(--ink); }
.invite-copy:hover { background: var(--paper-2); }
