:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1c2230;
  --muted: #5b6472;
  --border: #dde1e7;
  --primary: #3454d1;
  --primary-dark: #26399b;
  --success: #1e8e5a;
  --error: #c0392b;
  --header-bg: #eef0f4;
  --success-bg: #e3f5ea;
  --error-bg: #fbe9e7;
  --warn-bg: #fff4d6;
  --warn-text: #8a6d00;
  --radius: 8px;
}

/* Dark theme: applied when the user explicitly picks it, or when the OS
   prefers dark and the user hasn't overridden with a light choice. */
:root[data-theme="dark"] {
  --bg: #14171f;
  --surface: #1c212c;
  --text: #e8eaf0;
  --muted: #9aa3b5;
  --border: #2c3242;
  --primary: #6c8bff;
  --primary-dark: #8ba0ff;
  --success: #3ec37e;
  --error: #ef6b5c;
  --header-bg: #262c3b;
  --success-bg: #163828;
  --error-bg: #3a2220;
  --warn-bg: #3a3210;
  --warn-text: #e0c15c;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171f;
    --surface: #1c212c;
    --text: #e8eaf0;
    --muted: #9aa3b5;
    --border: #2c3242;
    --primary: #6c8bff;
    --primary-dark: #8ba0ff;
    --success: #3ec37e;
    --error: #ef6b5c;
    --header-bg: #262c3b;
    --success-bg: #163828;
    --error-bg: #3a2220;
    --warn-bg: #3a3210;
    --warn-text: #e0c15c;
  }
}

* { box-sizing: border-box; }

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

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.25rem;
}
.topbar-inner {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  max-width: 1200px;
  margin: 0 auto;
}
.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); }
.brand:hover { text-decoration: none; }
.topbar nav { display: flex; gap: 1rem; flex: 1; flex-wrap: wrap; }
.topbar nav a { color: var(--muted); font-weight: 600; font-size: 0.92rem; padding: 0.35rem 0; }
.topbar nav a:hover { color: var(--primary); text-decoration: none; }
.nav-toggle {
  display: none;
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 1.2rem;
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 3rem;
}

h1 { font-size: 1.6rem; margin: 0 0 1rem; }
h2 { font-size: 1.2rem; margin: 1.75rem 0 0.75rem; }
h3 { font-size: 1rem; margin: 1rem 0 0.5rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}

.table-scroll {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius);
}

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
}
th, td {
  text-align: left;
  padding: 0.55rem 0.7rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.92rem;
}
th { background: var(--header-bg); font-weight: 700; }
tr:last-child td { border-bottom: none; }

form.inline-form { display: inline; }

label { display: block; font-size: 0.85rem; font-weight: 600; margin-bottom: 0.25rem; color: var(--muted); }
input[type=text], input[type=email], input[type=tel], input[type=password],
input[type=number], input[type=date], select, textarea {
  width: 100%;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.95rem;
  background: var(--surface);
  color: var(--text);
}
.field { margin-bottom: 0.85rem; }
.field-row { display: flex; gap: 1rem; flex-wrap: wrap; }
.field-row .field { flex: 1; min-width: 160px; }

button, .button {
  display: inline-block;
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 6px;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  font-size: 0.9rem;
}
button:hover, .button:hover { background: var(--primary-dark); text-decoration: none; }
button.secondary, .button.secondary { background: var(--muted); }
button.danger, .button.danger { background: var(--error); }
button.small, .button.small { padding: 0.25rem 0.6rem; font-size: 0.8rem; }
.link-button {
  background: none; border: none; color: var(--primary); font-weight: 600;
  cursor: pointer; padding: 0; font-size: 0.9rem;
}
.link-button:hover { text-decoration: underline; }

.flashes { list-style: none; padding: 0; margin: 0 0 1rem; }
.flash { padding: 0.6rem 0.9rem; border-radius: 6px; margin-bottom: 0.5rem; font-size: 0.9rem; }
.flash-success { background: var(--success-bg); color: var(--success); }
.flash-error { background: var(--error-bg); color: var(--error); }

.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  background: var(--header-bg);
  color: var(--muted);
}
.badge-provisional { background: var(--warn-bg); color: var(--warn-text); }
.badge-final { background: var(--success-bg); color: var(--success); }

.stat-grid { display: flex; flex-wrap: wrap; gap: 1rem; }
.stat-card {
  flex: 1; min-width: 140px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem; text-align: center;
}
.stat-card .num { font-size: 2rem; font-weight: 800; color: var(--primary); }
.stat-card .label { color: var(--muted); font-size: 0.85rem; }

.heat-list { display: flex; flex-direction: column; gap: 0.75rem; }
.heat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.9rem; }
.heat-card.status-in_progress { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(52,84,209,0.15); }
.heat-card.status-completed { opacity: 0.65; }
.heat-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; }
.heat-entry-row { display: flex; align-items: center; gap: 0.5rem; padding: 0.3rem 0; border-bottom: 1px dashed var(--border); font-size: 0.9rem; }
.heat-entry-row:last-child { border-bottom: none; }
.heat-entry-row .grow { flex: 1; }

.score-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px,1fr)); gap: 0.75rem; align-items: end; }
.score-entry-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; background: var(--surface); }
.score-entry-card.scored { border-color: var(--success); background: var(--success-bg); }
.score-status { font-size: 0.8rem; font-weight: 700; }
.score-status.pending { color: var(--warn-text); }
.score-status.done { color: var(--success); }

.display-board { background: #0e1420; color: #f0f2f6; min-height: 100vh; padding: 2rem; }
.display-board h1 { color: #fff; }
.display-board table { background: #161d2c; }
.display-board th { background: #1f2740; color: #cbd3e6; }
.display-board td { border-color: #2a3350; color: #e7eaf2; }
.display-board .rank-1 td { background: #3a3110; }
.display-board .rank-2 td { background: #262b3a; }
.display-board .rank-3 td { background: #35271b; }

.display-mark {
  position: fixed;
  bottom: 1.25rem;
  right: 1.5rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  opacity: 0.55;
  color: #cbd3e6;
  font-size: 0.85rem;
  font-weight: 600;
}
.display-mark svg { width: 1.4rem; height: 1.4rem; }

@media print {
  .no-print { display: none !important; }
  body { background: #fff; }
  .container { max-width: 100%; padding: 0; }
}

.theme-toggle {
  background: var(--header-bg);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 0.35rem 0.7rem;
  font-size: 0.85rem;
  font-weight: 600;
}
.theme-toggle:hover { background: var(--border); }

.tap-input {
  font-size: 1.3rem;
  padding: 0.7rem;
  text-align: center;
}
.big-button {
  font-size: 1.1rem;
  padding: 0.9rem 1.4rem;
  width: 100%;
}

/* Backstage chat widget */
.chat-widget {
  position: fixed;
  right: 1.25rem;
  bottom: 1.25rem;
  z-index: 1000;
}
.chat-toggle {
  width: 3.5rem;
  height: 3.5rem;
  border-radius: 50%;
  font-size: 1.5rem;
  background: var(--primary);
  color: #fff;
  box-shadow: 0 4px 14px rgba(0,0,0,0.25);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}
.chat-toggle.has-unread { animation: chat-pulse 1s ease-in-out infinite; }
@keyframes chat-pulse {
  0%, 100% { box-shadow: 0 4px 14px rgba(0,0,0,0.25), 0 0 0 0 rgba(52,84,209,0.5); }
  50% { box-shadow: 0 4px 14px rgba(0,0,0,0.25), 0 0 0 10px rgba(52,84,209,0); }
}
.chat-badge {
  position: absolute;
  top: -0.35rem;
  right: -0.35rem;
  background: var(--error);
  color: #fff;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 800;
  min-width: 1.3rem;
  height: 1.3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 0.25rem;
}
.chat-panel {
  position: fixed;
  right: 1.25rem;
  bottom: 5.25rem;
  width: min(360px, calc(100vw - 2.5rem));
  height: min(480px, calc(100vh - 8rem));
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 28px rgba(0,0,0,0.3);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.chat-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.65rem 0.9rem;
  background: var(--header-bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.chat-panel-actions { display: flex; gap: 0.75rem; align-items: center; }
.chat-panel-actions .link-button { font-size: 1.1rem; }
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.chat-message { max-width: 85%; }
.chat-message.mine { align-self: flex-end; text-align: right; }
.chat-message .chat-bubble {
  display: inline-block;
  padding: 0.4rem 0.65rem;
  border-radius: 12px;
  background: var(--header-bg);
  font-size: 0.9rem;
  word-wrap: break-word;
  white-space: pre-wrap;
  text-align: left;
}
.chat-message.mine .chat-bubble { background: var(--primary); color: #fff; }
.chat-message .chat-meta { font-size: 0.72rem; color: var(--muted); margin: 0.1rem 0.2rem; }
.chat-form { display: flex; gap: 0.5rem; padding: 0.6rem; border-top: 1px solid var(--border); flex-shrink: 0; }

/* ---------------------------------------------------------------------
   Responsive: iPad (portrait ~768-1024px) and narrower laptop windows.
--------------------------------------------------------------------- */
@media (max-width: 860px) {
  .nav-toggle { display: inline-block; }
  .topbar-inner { flex-wrap: wrap; }
  .topbar nav {
    display: none;
    flex-basis: 100%;
    flex-direction: column;
    gap: 0;
    order: 3;
    border-top: 1px solid var(--border);
    margin-top: 0.5rem;
    padding-top: 0.5rem;
  }
  .topbar nav.open { display: flex; }
  .topbar nav a { padding: 0.6rem 0.2rem; }

  .container { padding: 1rem 0.85rem 2.5rem; }

  .stat-grid { gap: 0.6rem; }
  .stat-card { min-width: 45%; }

  .schedule-columns { flex-direction: column; }

  .chat-panel {
    right: 0.75rem;
    left: 0.75rem;
    width: auto;
    bottom: 4.75rem;
  }
  .chat-widget { right: 0.85rem; bottom: 0.85rem; }
}

/* Comfortable tap targets on touch devices (iPad, phones) regardless of
   viewport width, since an iPad in landscape can exceed 860px. */
@media (pointer: coarse) {
  button, .button, select, input[type=text], input[type=email], input[type=tel],
  input[type=password], input[type=number], input[type=date] {
    min-height: 44px;
  }
  .theme-toggle, .link-button { min-height: 44px; min-width: 44px; }
  .chat-panel-actions .link-button { min-width: 2.2rem; }
  th, td { padding: 0.7rem 0.7rem; }
}
.chat-form input { flex: 1; }

/* Rotating photo watermark behind the login cards. Each photo occupies a
   side zone (left or right third of the screen), alternating every
   rotation, so the centered card never overlaps the subject — a centered
   full-bleed image always puts the card right over the face no matter
   what vertical background-position is used, since the card sits at the
   horizontal center too. Keeping a clear middle gutter fixes that. */
.login-wallpaper {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  background: var(--bg);
}
.login-wallpaper .wallpaper-layer {
  position: absolute;
  top: 0;
  bottom: 0;
  /* Widest login card is 380px; 420px leaves a 40px buffer either side of
     it so this zone can never mathematically reach under the card,
     regardless of viewport width. */
  width: clamp(0px, calc((100vw - 420px) / 2), 480px);
  background-size: cover;
  background-position: 50% 15%;
  opacity: 0;
  transition: opacity 1.5s ease-in-out;
}
.login-wallpaper .wallpaper-layer.side-left { left: 0; }
.login-wallpaper .wallpaper-layer.side-right { right: 0; }
.login-wallpaper .wallpaper-layer.visible { opacity: 0.18; }
:root[data-theme="dark"] .login-wallpaper .wallpaper-layer.visible { opacity: 0.26; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .login-wallpaper .wallpaper-layer.visible { opacity: 0.26; }
}
@media (max-width: 640px) {
  /* Not enough clear width for a side photo without touching the card;
     the low-opacity base color still shows, just without the photo. */
  .login-wallpaper .wallpaper-layer { display: none; }
}
.login-card { position: relative; z-index: 1; }

.app-footer {
  position: relative;
  z-index: 1;
  max-width: 1200px;
  margin: 3rem auto 0;
  padding: 1.25rem 1.25rem 2rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}
.app-footer-icon { width: 1.1rem; height: 1.1rem; flex-shrink: 0; }

.print-header { margin-bottom: 1.5rem; }
.print-header svg { height: 40px; width: auto; display: block; }

.pin-slip {
  border: 2px solid var(--gold, #c9a227);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  max-width: 380px;
}
.pin-slip-role { font-size: 0.8rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); margin: 0 0 0.15rem; }
.pin-slip-name { font-size: 1.15rem; font-weight: 700; margin: 0 0 0.75rem; }
.pin-slip-pin { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-variant-numeric: tabular-nums; font-size: 2.2rem; font-weight: 800; letter-spacing: 0.12em; margin: 0 0 0.75rem; }
.pin-slip-note { font-size: 0.78rem; color: var(--muted); margin: 0 0 1rem; }
/* When a PIN slip is present, printing shows only the slip — not the rest
   of the admin page (add-member form, member table, etc). */
@media print {
  .pin-slip ~ * { display: none !important; }
}
