/* splitr — dark theme, mobile-safe.
 * Layered per website-design-handoff.md §2-4:
 *   1. tokens + reset (top)
 *   2. desktop layout (default layer)
 *   3. shared components
 *   4. mobile blocks at the END (last-rule wins)
 */

:root {
  --bg:         #0e1116;
  --bg-elev:    #161b22;
  --bg-elev-2:  #1c232c;
  --bg-elev-3:  #232b35;
  --border:     #2a313c;
  --border-strong: #3a4250;
  --text:       #e6edf3;
  --muted:      #8b949e;
  --accent:     #1f8f4d;
  --accent-2:   #34c77b;
  --highlight:  #ffd86b;
  --danger:     #f85149;
  --warn:       #d29922;
  --radius:     10px;
  --radius-sm:  6px;
  --radius-lg:  14px;
  --shadow:     0 6px 24px rgba(0,0,0,.35);
  --mono:       ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --font:       -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 14.5px/1.5 var(--font);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

a { color: var(--accent-2); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { margin: 0; font-size: 17px; font-weight: 600; }
h2 { margin: 0 0 4px; font-size: 22px; }
h3 { margin: 0 0 8px; font-size: 16px; color: var(--text); }
h4 { margin: 12px 0 6px; font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
small, .muted { color: var(--muted); }
.mono { font-family: var(--mono); }
.small { font-size: 12px; }
.warn { color: var(--warn); }

/* ─── Topbar ─────────────────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  padding-top: max(10px, env(safe-area-inset-top));
}
.topbar .brand {
  display: flex; align-items: center; gap: 10px;
  color: var(--text); text-decoration: none;
  min-width: 0;
}
.topbar .brand .logo { font-size: 22px; }
.topbar .brand h1 { font-size: 17px; margin: 0; font-weight: 600; }
.topnav { display: flex; gap: 12px; }

/* Avatar menu */
.avatar-menu { position: relative; }
.avatar {
  list-style: none; cursor: pointer;
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--accent); color: white;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 600; user-select: none;
}
.avatar::-webkit-details-marker { display: none; }
.avatar-menu .menu {
  position: absolute; right: 0; top: calc(100% + 8px);
  background: var(--bg-elev-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 220px;
  padding: 8px 0;
  z-index: 20;
}
.avatar-menu .menu a, .avatar-menu .menu .menu-button {
  display: block; width: 100%;
  padding: 8px 14px;
  color: var(--text); text-decoration: none;
  background: transparent; border: 0;
  font: inherit; text-align: left;
  cursor: pointer;
}
.avatar-menu .menu a:hover, .avatar-menu .menu .menu-button:hover {
  background: var(--bg-elev-3); text-decoration: none;
}
.menu-header {
  padding: 8px 14px 12px; border-bottom: 1px solid var(--border); margin-bottom: 6px;
}
.menu-header strong { display: block; }
.menu-header small { color: var(--muted); }

/* ─── Main / panels ──────────────────────────────────────────────────── */
.main {
  flex: 1;
  max-width: 920px;
  width: 100%;
  margin: 0 auto;
  padding: 24px 20px 80px;
}
.page-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 16px; margin-bottom: 20px;
}
.page-head h2 { margin-bottom: 2px; }
.head-actions { display: flex; gap: 8px; }

.panel {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin: 16px 0;
}
.empty {
  text-align: center;
  padding: 40px 20px;
  color: var(--muted);
}

/* ─── Forms ──────────────────────────────────────────────────────────── */
.auth-card {
  max-width: 380px; margin: 40px auto;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
}
.form-narrow { max-width: 480px; margin-left: auto; margin-right: auto; }
.form-inline { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
form label {
  display: block; margin: 12px 0;
  font-size: 13px; color: var(--muted);
}
form input, form select, form textarea {
  display: block; width: 100%;
  margin-top: 4px;
  padding: 9px 11px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  color: var(--text);
  font: inherit;
}
form input:focus, form select:focus {
  outline: none; border-color: var(--accent-2);
  box-shadow: 0 0 0 3px rgba(52, 199, 123, .15);
}
.row { display: flex; gap: 12px; }
.row .flex-1 { flex: 1; }
.row .flex-2 { flex: 2; }
.form-actions {
  display: flex; gap: 12px; justify-content: flex-end;
  margin-top: 20px;
}

/* Buttons */
.btn-primary, .btn-ghost, .btn-tiny, .btn-danger, button {
  font: inherit;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 1px solid transparent;
  padding: 8px 14px;
  text-decoration: none;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn-primary {
  background: var(--accent); color: white; border-color: var(--accent);
}
.btn-primary:hover { background: var(--accent-2); border-color: var(--accent-2); text-decoration: none; }
.btn-ghost {
  background: transparent; color: var(--text); border-color: var(--border-strong);
}
.btn-ghost:hover { background: var(--bg-elev-2); text-decoration: none; }
.btn-tiny {
  padding: 3px 9px; font-size: 12px;
  background: var(--bg-elev-3); border-color: var(--border-strong);
  color: var(--text);
}
.btn-tiny:hover { background: var(--bg-elev-2); text-decoration: none; }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--border-strong); }
.btn-danger:hover { background: rgba(248, 81, 73, .1); border-color: var(--danger); }

/* ─── Alerts ─────────────────────────────────────────────────────────── */
.alert {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  margin: 12px 0;
  font-size: 14px;
}
.alert.error   { background: rgba(248, 81, 73, .1); border: 1px solid var(--danger); color: #ffb4ad; }
.alert.success { background: rgba(31, 143, 77, .1); border: 1px solid var(--accent); }

.pill {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--bg-elev-3);
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  margin-left: 4px;
  white-space: nowrap;            /* never break the emoji + label */
  vertical-align: middle;
}
.pill.warn { background: rgba(210, 153, 34, .15); color: var(--warn); }

/* ─── Cards (group list) ─────────────────────────────────────────────── */
.cards { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; }
.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color .15s;
}
.card:hover { border-color: var(--border-strong); }
.card-link {
  display: block; padding: 14px 16px;
  color: var(--text); text-decoration: none;
}
.card-link:hover { text-decoration: none; }
.card-title { font-size: 16px; font-weight: 600; }
.card-meta { color: var(--muted); font-size: 12px; margin-top: 6px; display: flex; gap: 6px; }

/* ─── Tables ─────────────────────────────────────────────────────────── */
.data-table, .bal-table {
  width: 100%; border-collapse: collapse;
}
.data-table th, .data-table td, .bal-table th, .bal-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.data-table th, .bal-table th {
  color: var(--muted); font-weight: 500; font-size: 12px;
  text-transform: uppercase; letter-spacing: .04em;
}
.bal-table td:last-child { font-family: var(--mono); text-align: right; }
.bal-table tr.pos td:last-child { color: var(--accent-2); }
.bal-table tr.neg td:last-child { color: var(--danger); }

/* ─── Activity rows (expenses + payments) ────────────────────────────── */
.activity { list-style: none; padding: 0; margin: 0; }
.activity-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.activity-row:last-child { border-bottom: 0; }
.activity-main { flex: 1; min-width: 0; }
.activity-title { font-weight: 500; }
.activity-meta { font-size: 12px; }
.activity-amount { text-align: right; min-width: 110px; }
.activity-amount .mono { font-weight: 600; }

.suggest-list { list-style: none; padding: 0; margin: 8px 0 0; display: grid; gap: 6px; }
.suggest-list li {
  display: flex; gap: 10px; align-items: center;
  padding: 8px 10px;
  background: var(--bg-elev-2);
  border-radius: var(--radius-sm);
}
.suggest-list .mono { margin-left: auto; }

/* ─── Members & split UI ─────────────────────────────────────────────── */
.member-list { list-style: none; padding: 0; margin: 0; }
.member-list li {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 0; border-bottom: 1px solid var(--border);
}
.member-list li:last-child { border-bottom: 0; }

.split-box {
  margin: 18px 0; padding: 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg-elev-2);
}
.split-box legend { padding: 0 6px; color: var(--text); font-weight: 600; }
.member-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 6px;
  margin: 8px 0 12px;
}
.member-pill {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 8px;
  background: var(--bg-elev); border: 1px solid var(--border-strong);
  border-radius: 999px;
  cursor: pointer;
  font-size: 13px;
}
.member-pill input { width: auto; margin: 0; }
.split-mode { max-width: 240px; }
.share-grid { display: grid; gap: 6px; }
.share-row { display: grid; grid-template-columns: 1fr 110px; align-items: center; }
.share-row input { margin: 0; }
.share-row span { color: var(--text); }
#share-sum { margin-top: 8px; font-size: 12px; }
#share-sum.warn { color: var(--warn); }

.inline { display: inline; }

/* Swipe-to-archive cards on home (and swipe-to-delete on activity rows).
 *
 * Mobile: touch-drag the .swipe-front element left. Once past the
 *   threshold, the .swipe-action's form submits and the row goes away.
 *
 * Desktop: hover the .swipe-card to slide the front left and reveal
 *   the action button. Clicking the button performs the action.
 *   Mouse-drag is also wired (handy for testing) but not advertised.
 */
.swipe-card {
  position: relative;
  list-style: none;
  border-radius: var(--radius);
  /* Mobile: clip the dragged front. Desktop overrides this below so
   * popovers (⋯ menu, attachment popover) can extend beyond the card
   * boundary. */
  overflow: hidden;
  isolation: isolate;
}
.swipe-action {
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 110px;
  display: flex; align-items: stretch; justify-content: stretch;
  z-index: 0;
}
.swipe-archive { background: var(--danger); }
.swipe-delete  { background: var(--danger); }
.swipe-action-form { margin: 0; width: 100%; height: 100%; }
.swipe-action-btn {
  width: 100%; height: 100%;
  padding: 0 18px;
  border: none; background: transparent;
  color: white; font: inherit; font-weight: 600;
  cursor: pointer;
  font-size: 14px;
  white-space: nowrap;
}
.swipe-front {
  position: relative;
  z-index: 1;
  background: var(--bg-elev);   /* opaque so action doesn't show through */
  transform: translateX(0);
  transition: transform .15s ease-out;
  touch-action: pan-y;
}
.swipe-card.dragging .swipe-front { transition: none; }
.swipe-card.armed .swipe-front { transform: translateX(-110px); }

/* Desktop hover-reveal — only on fine-pointer devices (mice/trackpads).
 * Mobile users get the touch swipe instead.
 *
 * On desktop, we use a small ⋯ menu in the top-right corner of each
 * card instead of a hover-reveal slide-out. The swipe-action layer is
 * hidden entirely on desktop. We also drop `overflow: hidden` so the
 * ⋯ dropdown can escape the card boundary. */
@media (hover: hover) and (pointer: fine) {
  .swipe-card {
    overflow: visible;
    /* Without overflow:hidden the action layer would leak past the
     * card's rounded corners. Since we hide it on desktop anyway, this
     * is moot — but we belt-and-brace it. */
  }
  .swipe-card .swipe-action { display: none !important; }
  .swipe-card .swipe-front { transform: none !important; }
}

/* ⋯ menu in the top-right of each card on home. Desktop-only — on
 * mobile, the swipe gesture replaces it. */
.swipe-front .card-menu {
  position: absolute; top: 4px; right: 4px; z-index: 3;
  display: none;        /* mobile: hidden (swipe used instead) */
}
@media (hover: hover) and (pointer: fine) {
  .swipe-front .card-menu { display: inline-block; }
}
.swipe-front { position: relative; }
.card-menu-btn {
  list-style: none; cursor: pointer; user-select: none;
  padding: 2px 10px;
  color: var(--muted);
  font-size: 18px; line-height: 1; font-weight: 700;
  border-radius: var(--radius-sm);
  background: transparent;
}
.card-menu-btn::-webkit-details-marker { display: none; }
.card-menu-btn:hover {
  color: var(--text);
  background: var(--bg-elev-3);
}
.card-menu .menu {
  position: absolute; right: 0; top: calc(100% + 4px);
  background: var(--bg-elev-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 180px;
  padding: 6px 0;
}
.card-menu .menu .menu-button {
  display: block; width: 100%;
  padding: 8px 14px; background: transparent; border: 0;
  color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
.card-menu .menu .menu-button:hover { background: var(--bg-elev-3); }
/* Bump the card-link's right padding so the menu doesn't sit on top of
 * the group name when text is long. */
@media (hover: hover) and (pointer: fine) {
  .swipe-front .card-link { padding-right: 32px; }
}

/* For activity rows used as swipe targets, the row itself is the
 * `.swipe-front`. Keep its flex layout intact. Same overflow story as
 * group cards: clip on mobile so the drag is contained; on desktop
 * drop the clip so attachment popovers + edit menus can extend out. */
.activity-swipe .swipe-front { display: flex; align-items: center; gap: 12px; padding: 10px 12px; }
.activity-swipe + .activity-swipe { border-top: 1px solid var(--border); }
.activity-swipe { border-radius: 0; overflow: hidden; }
.activity-swipe:first-child { border-radius: var(--radius) var(--radius) 0 0; }
.activity-swipe:last-child  { border-radius: 0 0 var(--radius) var(--radius); }
@media (hover: hover) and (pointer: fine) {
  .activity-swipe { overflow: visible; }
  .activity-swipe .swipe-action { display: none !important; }
}

/* Quick-add bar at top of group page */
.quick-add {
  display: flex; gap: 8px; align-items: center;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 10px;
  margin: 0 0 16px;
}
.quick-add input[type=text] { flex: 1; margin: 0; padding: 7px 9px; }
.quick-add input[name=amount] { flex: 0 0 110px; }
.quick-add select[name=currency] { flex: 0 0 85px; margin: 0; padding: 7px 6px; }
.quick-add button { padding: 7px 14px; font-size: 18px; line-height: 1; }
.quick-err { color: var(--danger); font-size: 12px; padding: 0 6px; }

/* Activity icon column */
.activity-icon {
  flex: 0 0 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  background: var(--bg-elev-3);
  border-radius: 50%;
}
.activity-row.settlement .activity-amount .mono { color: var(--accent-2); }

.payers-box .primary-payer { display: block; margin: 8px 0; }
.payers-box .primary-payer select { margin: 0; }
.extra-payers { display: flex; flex-direction: column; gap: 6px; margin: 8px 0; }
.payer-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 110px 32px;
  gap: 8px; align-items: center;
}
.payer-row select, .payer-row input { margin: 0; }
.payer-row .remove-payer {
  width: 32px; height: 32px; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 16px; line-height: 1;
}
.payer-controls {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  margin-top: 8px;
}
.payer-controls #add-payer { padding: 6px 12px; }
#primary-share-hint { font-size: 12px; }
#primary-share-hint.warn { color: var(--warn); }

/* Attachment popover */
details.att-menu { position: relative; display: inline-block; }
details.att-menu summary {
  list-style: none; cursor: pointer; user-select: none;
}
details.att-menu summary::-webkit-details-marker { display: none; }
.att-pop {
  position: absolute; right: 0; top: calc(100% + 4px);
  background: var(--bg-elev-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 240px;
  padding: 8px;
  z-index: 25;
}
.att-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 4px 0;
  border-bottom: 1px solid var(--border);
}
.att-row:last-of-type { border-bottom: 0; }
.att-upload { margin-top: 6px; text-align: center; }
.att-upload label { cursor: pointer; }

/* Generic dropdown menu (used on group page for archive/restore/delete) */
details.group-menu { position: relative; display: inline-block; }
details.group-menu summary {
  list-style: none; cursor: pointer; user-select: none;
  padding: 6px 10px;
}
details.group-menu summary::-webkit-details-marker { display: none; }
details.group-menu .menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  background: var(--bg-elev-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 220px;
  padding: 6px 0;
  z-index: 20;
}
details.group-menu .menu .menu-button {
  display: block; width: 100%;
  padding: 8px 14px; background: transparent; border: 0;
  color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
details.group-menu .menu .menu-button:hover { background: var(--bg-elev-3); }
details.group-menu .menu .menu-button.danger { color: var(--danger); }
details.group-menu .menu .menu-button.danger:hover { background: rgba(248, 81, 73, .1); }

/* Edit + delete action cluster on activity rows */
.row-actions { display: flex; gap: 4px; align-items: center; }

/* Archived group cards */
.card.archived { opacity: .7; }
.card.archived .card-title { color: var(--muted); }

/* ─── Footer ─────────────────────────────────────────────────────────── */
.footer {
  border-top: 1px solid var(--border);
  padding: 10px 20px;
  color: var(--muted);
  font-size: 12px;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
}

/* ─── MOBILE — keep this block AT THE END so source-order wins ───────── */
@media (max-width: 640px) {
  .topbar {
    grid-template-columns: auto minmax(0, 1fr) auto;
    padding: 8px 12px;
    padding-top: max(8px, env(safe-area-inset-top));
    gap: 8px;
    min-height: 52px;
    overflow: hidden;
  }
  /* Pin each topbar child to its column EXPLICITLY. Without this,
   * `.topnav { display: none }` removes it from CSS Grid auto-placement
   * and `.topactions` slides into column 2, leaving the avatar floating
   * in the middle of the bar over the brand text. */
  .topbar .brand      { grid-column: 1; }
  .topbar .topnav     { grid-column: 2; }
  .topbar .topactions { grid-column: 3; justify-self: end; }
  .topbar .brand { min-width: 0; max-width: 60vw; overflow: hidden; }
  .topbar .brand .logo { font-size: 20px; }
  .topbar .brand h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 15px; }
  .topnav { display: none; }
  .avatar { width: 32px; height: 32px; font-size: 14px; }

  .main { padding: 14px 12px 80px; }
  .page-head { flex-direction: column; align-items: stretch; gap: 10px; }
  .head-actions { flex-wrap: wrap; }
  .head-actions .btn-primary, .head-actions .btn-ghost { flex: 1; justify-content: center; }
  .row { flex-direction: column; gap: 0; }
  .form-inline { flex-direction: column; align-items: stretch; }
  .form-inline label { width: 100%; }

  .panel { padding: 12px; }
  .activity-row { flex-wrap: wrap; }
  .activity-amount { min-width: auto; }

  .auth-card { margin: 16px 12px; padding: 18px; }

  .data-table, .bal-table { font-size: 13px; }
  .data-table th, .data-table td { padding: 6px 6px; }

  .member-grid { grid-template-columns: 1fr 1fr; }
  .share-row { grid-template-columns: 1fr 90px; }

  .quick-add { flex-wrap: wrap; }
  .quick-add input[name=amount] { flex: 1 1 110px; }
  .quick-add button { flex: 0 0 44px; }
}
