:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --border: #e3e6ec;
  --text: #1c2030;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-text: #ffffff;
  --success: #16a34a;
  --success-hover: #15803d;
  --danger: #b91c1c;
  --tag-bg: #eef2ff;
  --tag-text: #3730a3;
  --tag-pending-bg: #fef3c7;
  --tag-pending-text: #92400e;
  --tag-accepted-bg: #dcfce7;
  --tag-accepted-text: #166534;
  --tag-declined-bg: #fee2e2;
  --tag-declined-text: #991b1b;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.45;
  font-size: 16px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0.9rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
}

.nav {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.nav a {
  color: var(--text);
  text-decoration: none;
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
}
.nav a:hover { background: #f1f5f9; }

.nav .user {
  color: var(--muted);
  font-size: 0.85rem;
}

.badge {
  display: inline-block;
  background: var(--danger);
  color: white;
  border-radius: 999px;
  padding: 0 0.45rem;
  font-size: 0.72rem;
  font-weight: 700;
  min-width: 1.25rem;
  text-align: center;
  vertical-align: middle;
  margin-left: 0.2rem;
}

/* ---------- Layout ---------- */
.container {
  max-width: 880px;
  margin: 1rem auto 4rem;
  padding: 0 0.85rem;
}

h1 { margin: 0 0 0.4rem; font-size: 1.35rem; line-height: 1.2; }
h2 { margin: 0 0 0.65rem; font-size: 1.05rem; }
h3 { margin: 1rem 0 0.5rem; font-size: 0.95rem; }
.inline-h2 { display: inline; margin-right: 0.5rem; font-size: 1.05rem; }

p { margin: 0.5rem 0; }
.muted { color: var(--muted); font-size: 0.88rem; }
.error { color: var(--danger); font-size: 0.88rem; }
.errors { color: var(--danger); padding-left: 1.2rem; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.9rem 1rem;
  margin-bottom: 0.85rem;
  box-shadow: var(--shadow);
}

.card.narrow {
  max-width: 420px;
  margin-left: auto;
  margin-right: auto;
}

.header-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
  flex-wrap: wrap;
}

.header-title { flex: 1 1 auto; min-width: 0; }

.back-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  min-width: 40px;
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  text-decoration: none;
  flex: 0 0 auto;
}
.back-button svg { width: 16px; height: 16px; display: block; }
.back-button:hover { background: #f1f5f9; }

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

/* ---------- Buttons ---------- */
button, .button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.55rem 0.95rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
  font-size: 0.95rem;
  font-family: inherit;
  min-height: 40px;
  line-height: 1.2;
}

button:hover, .button:hover { background: #f1f5f9; }

button.primary, .primary {
  background: var(--primary);
  color: var(--primary-text);
  border-color: var(--primary);
}
button.primary:hover, .primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }

button.success, .success {
  background: var(--success);
  color: white;
  border-color: var(--success);
  font-weight: 600;
  letter-spacing: 0.02em;
}
button.success:hover, .success:hover { background: var(--success-hover); border-color: var(--success-hover); }

button.danger, .danger {
  border-color: var(--danger);
  color: var(--danger);
  background: var(--card);
}
button.danger:hover { background: #fef2f2; }

button.block, .block { display: flex; width: 100%; padding: 0.85rem 1rem; font-size: 1.05rem; }

.button.disabled {
  opacity: 0.5;
  pointer-events: none;
}

.link-button {
  background: none;
  border: none;
  color: var(--primary);
  padding: 0.25rem 0.4rem;
  text-decoration: underline;
  font-size: 0.88rem;
  cursor: pointer;
  min-height: auto;
  border-radius: 4px;
}
.link-button.danger { color: var(--danger); text-decoration: none; font-size: 1.1rem; }

.star-form { display: inline-flex; align-items: center; }
.star-button {
  background: none;
  border: none;
  padding: 0.25rem 0.35rem;
  font-size: 1.1rem;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  min-height: auto;
  border-radius: 6px;
}
.star-button:hover { background: #f1f5f9; color: #d97706; }
.star-button.active { color: #f59e0b; }
.star-button.active:hover { color: #b45309; }
.star-button.large {
  font-size: 1.4rem;
  padding: 0.45rem 0.65rem;
  min-height: 40px;
  min-width: 40px;
  border: 1px solid var(--border);
  background: var(--card);
}
.star-button.large:hover { background: #f1f5f9; }

form.inline { display: inline; }

/* ---------- Inputs ---------- */
input[type="text"], input[type="password"], input[type="number"] {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  width: 100%;
  background: white;
}

input[type="checkbox"] {
  width: 22px;
  height: 22px;
  margin: 0;
  cursor: pointer;
}

label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.qty-input { width: 5.5rem; flex: 0 0 auto; }

.row-form {
  display: flex;
  gap: 0.5rem;
  align-items: stretch;
  flex-wrap: wrap;
}
.row-form input[type="text"] { flex: 1 1 200px; min-width: 0; }
.row-form button { flex: 0 0 auto; }

/* ---------- Messages ---------- */
.messages { list-style: none; padding: 0; margin: 0 0 0.75rem; }
.messages .message {
  padding: 0.55rem 0.85rem;
  border-radius: 8px;
  margin-bottom: 0.4rem;
  font-size: 0.92rem;
  background: #ecfeff;
  border: 1px solid #a5f3fc;
}
.messages .message.error { background: #fef2f2; border-color: #fecaca; color: var(--danger); }
.messages .message.success { background: #ecfdf5; border-color: #bbf7d0; color: #065f46; }
.messages .message.info { background: #eff6ff; border-color: #bfdbfe; color: #1e3a8a; }

/* ---------- Settings gear menu (uses <details>) ---------- */
.settings-menu {
  position: relative;
}
.settings-menu summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  min-height: 40px;
  min-width: 40px;
  font-size: 1.2rem;
}
.settings-menu summary::-webkit-details-marker { display: none; }
.settings-menu summary:hover { background: #f1f5f9; }
.settings-menu .settings-menu-content {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(15,23,42,0.12);
  min-width: 180px;
  padding: 0.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  z-index: 4;
}
.settings-menu .settings-menu-content > a,
.settings-menu .settings-menu-content > form > .menu-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text);
  padding: 0.55rem 0.75rem;
  border-radius: 6px;
  text-decoration: none;
  font-size: 0.95rem;
  cursor: pointer;
  min-height: auto;
}
.settings-menu .settings-menu-content > a:hover,
.settings-menu .settings-menu-content > form > .menu-item:hover {
  background: #f1f5f9;
}
.settings-menu .settings-menu-content .menu-item.danger { color: var(--danger); }

.user-menu > summary {
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.35rem 0.6rem;
  gap: 0.3rem;
  min-width: 0;
  color: var(--text);
}
.user-menu .user-menu-caret {
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1;
}

/* ---------- List overview ---------- */
.list-links {
  list-style: none;
  padding: 0;
  margin: 0;
}
.list-links li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 0;
  border-bottom: 1px solid var(--border);
}
.list-links li:last-child { border-bottom: none; }
.list-links a {
  color: var(--text);
  text-decoration: none;
  font-weight: 500;
  flex: 1 1 auto;
}
.list-links a:hover { color: var(--primary); }

.tag {
  display: inline-block;
  background: var(--tag-bg);
  color: var(--tag-text);
  font-size: 0.68rem;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.tag-pending  { background: var(--tag-pending-bg);  color: var(--tag-pending-text); }
.tag-accepted { background: var(--tag-accepted-bg); color: var(--tag-accepted-text); }
.tag-declined { background: var(--tag-declined-bg); color: var(--tag-declined-text); }

.invites { list-style: none; padding: 0; margin: 0; }
.invite-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0;
  border-bottom: 1px solid var(--border);
  gap: 0.75rem;
  flex-wrap: wrap;
}
.invite-row:last-child { border-bottom: none; }

/* ---------- Items (card-style on mobile) ---------- */
.items-list {
  list-style: none;
  padding: 0;
  margin: 0 0 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.item-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas:
    "check edit delete"
    ".     meta meta";
  column-gap: 0.6rem;
  row-gap: 0.25rem;
  align-items: center;
  padding: 0.6rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fbfcfe;
}
.item-row.bought {
  background: #f4faf6;
  border-color: #c8e6d2;
}
.item-row.bought .item-edit-form input[type="text"] {
  text-decoration: line-through;
  color: var(--muted);
}

.item-toggle    { grid-area: check;  display: flex; align-items: center; justify-content: center; }
.item-edit-form { grid-area: edit;   min-width: 0; }
.item-delete    { grid-area: delete; display: flex; align-items: center; justify-content: center; }
.item-meta      { grid-area: meta;   font-size: 0.82rem; }
.item-meta small { font-size: 0.82rem; }

.item-edit-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.4rem;
  align-items: center;
}
.item-edit-form input[type="text"] { min-width: 0; }
.item-edit-form .qty-input { width: 4.5rem; }

/* Enable horizontal drag-scroll on item name inputs (handler in list_detail.html).
   pan-y lets the browser keep vertical page scroll while we capture horizontal gestures. */
.item-edit-form input[name="name"],
.row-form input[name="name"] { touch-action: pan-y; }

/* Expand-button affordance for overflowing item-name fields. Wrapper is added
   at runtime; it inherits the input's flex/grid slot in its parent form. */
.input-wrap { position: relative; display: block; min-width: 0; }
.row-form .input-wrap { flex: 1 1 200px; min-width: 0; }
.input-wrap > input[type="text"],
.input-wrap > textarea { width: 100%; }
.input-wrap.has-overflow > input[type="text"],
.input-wrap.is-expanded > textarea { padding-right: 2.4rem; }
.input-wrap > textarea {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  background: white;
  resize: none;
  display: block;
  line-height: 1.35;
  overflow: hidden;
  white-space: pre-wrap;
  word-break: break-word;
}
.input-expand-btn {
  position: absolute;
  top: 1px;
  right: 1px;
  width: 2rem;
  height: 2.35rem;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: 0 7px 7px 0;
  font-size: 0.9rem;
  line-height: 1;
}
.input-wrap.has-overflow > .input-expand-btn,
.input-wrap.is-expanded > .input-expand-btn { display: flex; }
.input-expand-btn:hover { color: var(--primary); background: rgba(37, 99, 235, 0.08); }
.input-expand-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }

.item-delete .link-button.danger {
  font-size: 1.4rem;
  padding: 0.25rem 0.5rem;
  line-height: 1;
}

.done-form { margin-top: 0.5rem; }

/* ---------- Members ---------- */
.members-card { padding-top: 0; padding-bottom: 0; }
.members-card > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.9rem 0;
}
.members-card > summary::-webkit-details-marker { display: none; }
.members-card > summary::after {
  content: '\25BE';
  color: var(--muted);
  transition: transform 0.15s ease;
}
.members-card[open] > summary::after { transform: rotate(180deg); }
.members-card[open] { padding-bottom: 1rem; }

.members { list-style: none; padding: 0; margin: 0; }
.members li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.members li:last-child { border-bottom: none; }
.members .member-row > div {
  display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap;
}

/* ---------- History page ---------- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.65rem;
  margin-bottom: 0.85rem;
}
.stat { text-align: center; padding: 0.85rem 0.5rem; }
.stat-num { font-size: 1.65rem; font-weight: 700; color: var(--primary); }
.stat-label { color: var(--text); font-size: 0.85rem; font-weight: 500; }
.stat-sublabel { color: var(--muted); font-size: 0.72rem; margin-top: 0.1rem; }

.year-form select,
.page-size-form select {
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  font-size: 0.95rem;
  font-family: inherit;
  min-height: 40px;
  cursor: pointer;
}

.card-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-bottom: 0.5rem;
}
.card-header-row h2 { margin: 0; }

.page-size-form {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.page-size-form label {
  margin: 0;
  font-size: 0.85rem;
}
.page-size-form select { min-height: 36px; padding: 0.3rem 0.5rem; }

.bar-chart {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.bar-row {
  display: grid;
  grid-template-columns: 4.2rem 1fr 2rem;
  gap: 0.5rem;
  align-items: center;
  font-size: 0.85rem;
}
.bar-label { color: var(--muted); }
.bar-track {
  background: #eef2f7;
  border-radius: 6px;
  height: 18px;
  overflow: hidden;
}
.bar-fill {
  display: block;
  background: linear-gradient(90deg, #60a5fa, var(--primary));
  height: 100%;
  border-radius: 6px;
  min-width: 2px;
}
.bar-count { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }

.trip-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.45rem; }

details.trip {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fbfcfe;
  padding: 0.55rem 0.75rem;
}
details.trip > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.6rem;
  flex-wrap: wrap;
}
details.trip > summary::-webkit-details-marker { display: none; }
details.trip > summary::after {
  content: '\25BE';
  color: var(--muted);
  margin-left: auto;
}
details.trip[open] > summary::after { content: '\25B4'; }
.trip-date { font-weight: 600; }
.trip-meta { color: var(--muted); font-size: 0.85rem; }
.trip-items {
  list-style: none;
  padding: 0.5rem 0 0;
  margin: 0.4rem 0 0;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}
.trip-item-name { font-weight: 500; }

.pagination {
  margin-top: 0.85rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* ---------- Narrow-screen tweaks ---------- */
@media (max-width: 520px) {
  body { font-size: 15px; }
  .container { margin-top: 0.5rem; padding: 0 0.6rem; }
  .card { padding: 0.8rem 0.85rem; border-radius: 10px; }
  h1 { font-size: 1.2rem; }
  .header-actions { width: 100%; justify-content: flex-end; }
  .header-actions .button { padding: 0.45rem 0.7rem; font-size: 0.9rem; }

  .item-row {
    grid-template-columns: auto 1fr auto;
    column-gap: 0.5rem;
    row-gap: 0.2rem;
  }
  .item-edit-form { grid-template-columns: 1fr auto; }
  .item-edit-form .qty-input { width: 4rem; }

  .row-form input[type="text"] { flex-basis: 100%; }
  .row-form button { flex: 1 1 auto; }

  .stat-num { font-size: 1.4rem; }
  .bar-row { grid-template-columns: 4rem 1fr 1.6rem; font-size: 0.78rem; }

  .messages .message { font-size: 0.88rem; }
}

@media (min-width: 720px) {
  .item-edit-form {
    grid-template-columns: 1fr 5rem;
  }
}
