:root {
  /* Dark is the default. Light is applied below when the OS asks for it and
     the user hasn't pinned a theme, or when they pick Light on the home
     screen (data-theme="light" / "dark", set from app.js). */
  color-scheme: dark;
  --bg: #14181f;
  --card: #1e242e;
  --card-2: #262e3a;
  --text: #e8ecf1;
  --muted: #8b96a5;
  --accent: #f5a623;
  --accent-text: #1a1206;
  --accent-ink: #f5a623;   /* amber used as text/icon colour on the page bg */
  --green: #4cc38a;
  --red: #e5545e;
  --border: #313a48;
  --radius: 14px;
}

/* Light palette. Listed twice so a system-light default and an explicit
   "Light" pick both resolve, while an explicit "Dark" pick always wins. */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #f4f6f9;
    --card: #ffffff;
    --card-2: #eaeef3;
    --text: #1b2029;
    --muted: #5c6773;
    --accent: #f5a623;
    --accent-text: #1a1206;
    --accent-ink: #915a00;
    --green: #1f9d63;
    --red: #d23843;
    --border: #d9dee6;
  }
}
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f6f9;
  --card: #ffffff;
  --card-2: #eaeef3;
  --text: #1b2029;
  --muted: #5c6773;
  --accent: #f5a623;
  --accent-text: #1a1206;
  --accent-ink: #915a00;
  --green: #1f9d63;
  --red: #d23843;
  --border: #d9dee6;
}

/* ---------- pull to refresh ---------- */
.ptr {
  position: fixed;
  top: calc(10px + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translate(-50%, -50px);
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  pointer-events: none;
  transition: transform 0.15s ease-out;
}
.ptr-spinner {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--muted);
}
.ptr.ptr-ready .ptr-spinner { border-top-color: var(--accent); }
.ptr.ptr-spinning { transition: none; }
.ptr.ptr-spinning .ptr-spinner {
  border-top-color: var(--accent);
  animation: ptr-spin 0.7s linear infinite;
}
@keyframes ptr-spin {
  to { transform: rotate(360deg); }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  /* Keep the browser's own pull-to-refresh / scroll-chaining out of the way of
     the app's custom gesture (and of a native shell's scroll bounce). */
  overscroll-behavior-y: contain;
}

/* Insets keep content clear of the status bar, camera notch, and home
   indicator when installed to the home screen (viewport-fit=cover). */
#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  /* Plain padding first: browsers without env() keep this and skip the rest. */
  padding: 16px 16px 24px;
  padding-top: calc(16px + env(safe-area-inset-top, 0px));
  padding-right: calc(16px + env(safe-area-inset-right, 0px));
  padding-bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  padding-left: calc(16px + env(safe-area-inset-left, 0px));
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0 18px;
}
.topbar h1 {
  font-size: 1.15rem;
  font-weight: 700;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.back-btn {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  width: 38px;
  height: 38px;
  font-size: 1.1rem;
  cursor: pointer;
  flex-shrink: 0;
}

.brand {
  position: relative;
  text-align: center;
  padding: 18px 0 6px;
}
.theme-btn {
  position: absolute;
  top: 6px;
  right: 0;
  width: 38px;
  height: 38px;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  font-size: 1.05rem;
  line-height: 1;
  cursor: pointer;
}
.brand h1 { font-size: 1.5rem; font-weight: 800; margin-top: 4px; }

.sync-line {
  color: var(--muted);
  font-size: 0.8rem;
  margin: -8px 0 14px;
}

/* ---------- cards & lists ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.list-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 10px;
  width: 100%;
  color: var(--text);
  font-size: 1rem;
  text-align: left;
  cursor: pointer;
}
.list-item .grow { flex: 1; min-width: 0; }
.list-item .sub { color: var(--muted); font-size: 0.82rem; margin-top: 2px; }
.list-item .chev { color: var(--muted); }

/* ---------- standings ---------- */
.standing {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 4px;
  border-bottom: 1px solid var(--border);
}
.standing:first-child { padding-top: 0; }
.standing:last-child { border-bottom: none; padding-bottom: 0; }
.standing .name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.standing .name .sub { color: var(--muted); font-size: 0.78rem; margin-top: 1px; }
.standing.up-next .name { color: var(--accent-ink); font-weight: 700; }
.balance { font-weight: 700; font-size: 0.8rem; white-space: nowrap; text-align: right; }
.balance.pos { color: var(--green); }
.balance.neg { color: var(--red); }
.balance.zero { color: var(--muted); }

.section-title {
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin: 18px 0 10px;
}

/* A section heading with an action (e.g. Select all) on the right. */
.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin: 18px 0 10px;
}
.section-head .section-title { margin: 0; }
.section-head button {
  background: none;
  border: none;
  color: var(--accent-ink);
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  padding: 2px 4px;
}

/* ---------- buttons ---------- */
.btn {
  display: block;
  width: 100%;
  padding: 15px;
  border-radius: var(--radius);
  border: none;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  margin-bottom: 10px;
}
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-secondary {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  font-weight: 600;
}
.btn-danger { background: var(--red); color: #fff; }
.btn-danger-ghost {
  background: none;
  border: none;
  color: var(--red);
  font-size: 0.9rem;
  cursor: pointer;
  padding: 8px;
}
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; font-size: 0.95rem; padding: 14px 8px; }

.icon-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
  padding: 6px;
}

/* ---------- forms ---------- */
input[type="text"] {
  width: 100%;
  padding: 13px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card-2);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 10px;
}
input[type="text"]:focus { outline: 2px solid var(--accent); border-color: transparent; }

select {
  width: 100%;
  padding: 13px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card-2);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 10px;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%238b96a5' stroke-width='2' fill='none'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
}
select:focus { outline: 2px solid var(--accent); border-color: transparent; }

.pick-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 13px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 8px;
  cursor: pointer;
  text-align: left;
}
.pick-item.selected {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--card));
}
.pick-item .mark {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--muted);
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
}
.pick-item[data-part] .mark { border-radius: 6px; }
.pick-item.selected .mark {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-text);
}
.pick-item .grow { flex: 1; }
.pick-item .tag { color: var(--muted); font-size: 0.78rem; }

/* ---------- history ---------- */
.shout-entry {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 4px;
  border-bottom: 1px solid var(--border);
}
.shout-entry:first-child { padding-top: 0; }
.shout-entry:last-child { border-bottom: none; padding-bottom: 0; }
.shout-entry .grow { flex: 1; min-width: 0; }
.shout-entry .buyer { font-weight: 700; }
.shout-entry .detail { color: var(--muted); font-size: 0.82rem; margin-top: 3px; overflow-wrap: anywhere; }

.note {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.5;
  margin-bottom: 12px;
}
.note strong { color: var(--text); }
.note .btn { margin-bottom: 0; }
.note .btn-danger-ghost { display: block; margin: 6px auto 0; }

.empty {
  text-align: center;
  color: var(--muted);
  padding: 30px 16px;
  font-size: 0.92rem;
  line-height: 1.5;
}

/* ---------- toast ---------- */
.toast {
  position: fixed;
  bottom: 20px;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  background: var(--card-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 999px;
  padding: 10px 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 6px 24px rgba(0,0,0,0.45);
  font-size: 0.92rem;
  z-index: 10;
}
.toast button {
  background: none;
  border: none;
  color: var(--accent-ink);
  font-weight: 700;
  font-size: 0.92rem;
  cursor: pointer;
}

/* ---------- focus ---------- */
/* Keyboard focus needs to be visible on every interactive element, not just
   the text inputs. :focus-visible keeps it off taps/clicks. */
.btn:focus-visible,
.back-btn:focus-visible,
.theme-btn:focus-visible,
.list-item:focus-visible,
.pick-item:focus-visible,
.icon-btn:focus-visible,
.btn-danger-ghost:focus-visible,
.section-head button:focus-visible,
.toast button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
#app h1:focus-visible { outline: none; } /* programmatic focus target only */

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 100;
}
.modal {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}
.modal p {
  margin-bottom: 16px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}
.modal input[type="text"] { margin-bottom: 16px; }
.modal .btn-row { margin: 0; }
.modal .btn-row .btn { margin-bottom: 0; }
