/* ============ Design tokens ============ */
:root {
  --bg: #f3f5f9;
  --card: #ffffff;
  --ink: #142033;
  --muted: #64748b;
  --line: #e2e8f0;
  --field: #f8fafc;
  --brand: #0e8a65;
  --brand-dark: #0b6e51;
  --brand-soft: #e3f5ee;
  --accent: #2563eb;
  --danger: #dc2626;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 8px 24px rgba(15, 23, 42, .06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1420;
    --card: #151f2e;
    --ink: #e6edf7;
    --muted: #94a3b8;
    --line: #26354a;
    --field: #0f1826;
    --brand: #22c08e;
    --brand-dark: #17a679;
    --brand-soft: #123528;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

h1, h2 { margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 17px; font-weight: 700; margin-bottom: 18px; }
code { background: var(--field); border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; font-size: 13px; }
.muted { color: var(--muted); line-height: 1.6; }

/* ============ Header ============ */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 22px clamp(16px, 4vw, 40px);
  background: linear-gradient(120deg, #0b6e51 0%, #0e8a65 55%, #14a37a 100%);
  color: #fff;
}
.brand { display: flex; align-items: center; gap: 14px; flex: 1; min-width: 0; }
.logo {
  width: 46px; height: 46px; border-radius: 12px;
  display: grid; place-items: center;
  background: rgba(255, 255, 255, .18);
  font-weight: 800; font-size: 22px;
}
.brand h1 { font-size: 22px; font-weight: 800; }
.brand p { margin: 2px 0 0; opacity: .85; font-size: 13px; }

.chest {
  text-align: right;
  background: rgba(255, 255, 255, .14);
  border: 1px solid rgba(255, 255, 255, .25);
  border-radius: 12px;
  padding: 8px 16px;
}
.chest span { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; opacity: .85; }
.chest strong { font-size: 30px; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }

.spinner {
  display: none;
  width: 26px; height: 26px;
  border: 3px solid rgba(255, 255, 255, .35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ============ Tabs ============ */
.tabs {
  display: flex;
  gap: 4px;
  padding: 0 clamp(16px, 4vw, 40px);
  background: var(--card);
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}
.tab {
  border: 0;
  background: none;
  padding: 14px 16px;
  font: inherit;
  font-weight: 600;
  color: var(--muted);
  border-bottom: 3px solid transparent;
  cursor: pointer;
  white-space: nowrap;
}
.tab:hover { color: var(--ink); }
.tab.active { color: var(--brand); border-bottom-color: var(--brand); }

main { max-width: 1040px; margin: 0 auto; padding: 24px clamp(16px, 4vw, 40px) 60px; }
.panel { display: none; }
.panel.active { display: block; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(18px, 3vw, 28px);
  margin-bottom: 22px;
}

/* ============ Form layout ============ */
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 18px 22px; }
.grid.three { grid-template-columns: repeat(3, 1fr); }
.span-2 { grid-column: span 2; }
.span-3 { grid-column: 1 / -1; }
@media (max-width: 720px) {
  .grid, .grid.three { grid-template-columns: 1fr; }
  .span-2, .span-3 { grid-column: auto; }
}

.field > label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: 7px;
}

input[type=text], input[type=number], input[type=password], select {
  width: 100%;
  height: 44px;
  padding: 0 14px;
  font: inherit;
  color: var(--ink);
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
  transition: border-color .15s, box-shadow .15s, background .15s;
}
input[type=text]:focus, input[type=number]:focus, input[type=password]:focus, select:focus {
  border-color: var(--brand);
  background: var(--card);
  box-shadow: 0 0 0 4px var(--brand-soft);
}
#fname { height: 50px; font-size: 18px; font-weight: 500; }

.school-row { display: grid; grid-template-columns: 1fr 1.3fr auto; gap: 10px; }
@media (max-width: 720px) { .school-row { grid-template-columns: 1fr; } }

/* Segmented control (gender, category) */
.segmented {
  display: inline-flex;
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 3px;
}
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label {
  padding: 9px 18px;
  border-radius: 8px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: background .15s, color .15s;
  user-select: none;
}
.segmented input:checked + label { background: var(--brand); color: #fff; box-shadow: 0 2px 6px rgba(14, 138, 101, .35); }
.segmented input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Checkbox chips (rinks, report columns) */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips input { position: absolute; opacity: 0; pointer-events: none; }
.chips label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--field);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  transition: all .15s;
}
.chips label::before {
  content: "";
  width: 16px; height: 16px;
  border: 2px solid var(--line);
  border-radius: 5px;
  background: var(--card);
  transition: all .15s;
}
.chips input:checked + label { border-color: var(--brand); background: var(--brand-soft); color: var(--brand-dark); }
.chips input:checked + label::before {
  border-color: var(--brand);
  background: var(--brand) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='white' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 12px no-repeat;
}
.chips input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (prefers-color-scheme: dark) { .chips input:checked + label { color: var(--brand); } }

/* Toggle switch */
.switch { display: inline-flex; align-items: center; gap: 12px; font-weight: 600; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; }
.switch .track {
  width: 44px; height: 26px;
  border-radius: 999px;
  background: var(--line);
  position: relative;
  transition: background .2s;
}
.switch .track::after {
  content: "";
  position: absolute; top: 3px; left: 3px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .25);
  transition: transform .2s;
}
.switch input:checked + .track { background: var(--brand); }
.switch input:checked + .track::after { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ============ Buttons ============ */
.actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 10px; margin-top: 24px; }
.row-gap { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  height: 44px;
  padding: 0 20px;
  border-radius: 10px;
  border: 1px solid transparent;
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, transform .05s, box-shadow .15s;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--brand); color: #fff; box-shadow: 0 4px 14px rgba(14, 138, 101, .3); }
.btn.primary:hover { background: var(--brand-dark); }
.btn.ghost { background: var(--card); color: var(--ink); border-color: var(--line); }
.btn.ghost:hover { background: var(--field); }
.btn[disabled] { opacity: .55; cursor: wait; }
#register { height: 50px; padding: 0 28px; font-size: 16px; }
kbd {
  font: 600 11px/1 "Inter", sans-serif;
  padding: 4px 6px;
  border-radius: 5px;
  background: rgba(255, 255, 255, .2);
}

/* ============ Tables ============ */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
  text-align: left;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  padding: 10px 12px;
  border-bottom: 2px solid var(--line);
  white-space: nowrap;
}
.table td { padding: 10px 12px; border-bottom: 1px solid var(--line); }
.table tbody tr:hover { background: var(--field); }
.table td:first-child { font-weight: 700; font-variant-numeric: tabular-nums; }
.pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pill.ok { background: var(--brand-soft); color: var(--brand-dark); }
.pill.no { background: #fdecec; color: var(--danger); }

.errors {
  margin-top: 16px;
  padding: 14px;
  border-radius: 10px;
  background: #fdecec;
  color: #991b1b;
  white-space: pre-wrap;
  font-size: 13px;
}

/* ============ Report preview ============ */
.report-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.report-head h2 { margin: 0; font-size: 20px; }
.report-meta { color: var(--muted); font-size: 13px; margin: 6px 0 18px; }
.report-group { margin-bottom: 28px; }
.report-group h3 {
  font-size: 15px;
  margin: 0 0 8px;
  padding: 8px 12px;
  border-left: 4px solid var(--brand);
  background: var(--brand-soft);
  border-radius: 0 8px 8px 0;
}
.report-group h3 small { color: var(--muted); font-weight: 500; margin-left: 6px; }
.report .table td:first-child { font-weight: 500; color: var(--muted); }
.report tfoot td { font-weight: 700; border-top: 2px solid var(--line); }

/* ============ Modal & toast ============ */
.modal {
  display: none;
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, .45);
  backdrop-filter: blur(2px);
  align-items: center; justify-content: center;
  padding: 16px;
  z-index: 30;
}
.modal.open { display: flex; }
.modal-box {
  width: min(420px, 100%);
  background: var(--card);
  border-radius: var(--radius);
  padding: 26px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}

#toast {
  position: fixed;
  left: 50%; bottom: 28px;
  transform: translate(-50%, 20px);
  max-width: calc(100% - 32px);
  background: #0f5132;
  color: #fff;
  padding: 13px 20px;
  border-radius: 12px;
  font-weight: 600;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .25);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 40;
}
#toast.error { background: var(--danger); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ============ Print: only the report ============ */
@media print {
  @page { size: A4 landscape; margin: 12mm; }
  :root { --ink: #000; --muted: #444; --line: #bbb; --card: #fff; --field: #fff; --brand-soft: #eef7f3; }
  body { background: #fff; font-size: 11pt; }
  .topbar, .tabs, #tab-register, #tab-admin, #tab-reports > .card:first-child, #toast, .modal { display: none !important; }
  main { padding: 0; max-width: none; }
  #tab-reports, #report_out { display: block !important; }
  .card { border: 0; box-shadow: none; padding: 0; }
  .report-group { break-inside: auto; }
  .report-group + .report-group { break-before: page; }
  .table th, .table td { padding: 5px 8px; }
  .table thead { display: table-header-group; }
  .table tr { break-inside: avoid; }
}

/* The fee / summary switches sit inside .field but are not headings */
.field > label.switch {
  display: inline-flex;
  font-size: 15px;
  text-transform: none;
  letter-spacing: 0;
  color: var(--ink);
  margin: 0;
  min-height: 44px;
}
.switch .track { flex: none; }

/* ============ Events & admin ============ */
.banner {
  margin-bottom: 18px;
  padding: 14px 18px;
  border-radius: 12px;
  background: #fff4e5;
  border: 1px solid #f5c47a;
  color: #8a4b00;
  font-weight: 600;
}
form.locked { opacity: .5; pointer-events: none; }

.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 6px; }
.card-head h2 { margin: 0; }
.small-text { font-size: 13px; font-weight: 500; }

.narrow { max-width: 400px; margin: 40px auto; }
.center { text-align: center; }
.lock-icon { font-size: 34px; margin-bottom: 8px; }
.pin {
  display: block;
  margin: 0 auto 12px;
  height: 56px !important;
  text-align: center;
  font-size: 26px !important;
  letter-spacing: .4em;
}
.btn.wide { width: 100%; margin-top: 6px; }

.btn.small { height: 34px; padding: 0 12px; font-size: 13px; border-radius: 8px; }
.btn.danger { background: var(--card); color: var(--danger); border-color: #f3b4b4; }
.btn.danger:hover { background: #fdecec; }
.btn.danger[disabled] { cursor: not-allowed; }

.row-actions { white-space: nowrap; text-align: right; }
.row-actions .btn { margin-left: 4px; }
.active-row { background: var(--brand-soft); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 6px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; }
.badge.brand { background: var(--brand); color: #fff; }

.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin: 16px 0 8px; flex-wrap: wrap; }
.check { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; cursor: pointer; }
.check input, .m-pick { width: 18px; height: 18px; accent-color: var(--brand); cursor: pointer; }
.scroll-y { max-height: 480px; overflow-y: auto; }
.scroll-y thead th { position: sticky; top: 0; background: var(--card); }
.sep { border: 0; border-top: 1px solid var(--line); margin: 28px 0 22px; }

@media (prefers-color-scheme: dark) {
  .banner { background: #3a2a10; border-color: #7a5a20; color: #f5c47a; }
  .btn.danger { border-color: #6b2b2b; }
  .btn.danger:hover { background: #3a1717; }
}

/* Dropdowns: own arrow with room on the right, so it never touches the border */
select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  padding-right: 42px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4 6l4 4 4-4' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  background-size: 16px;
  text-overflow: ellipsis;
  cursor: pointer;
}
select::-ms-expand { display: none; }
@media (prefers-color-scheme: dark) {
  select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4 6l4 4 4-4' fill='none' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  }
}

/* Find contestant / collect fee */
.find-bar { display: grid; grid-template-columns: 1fr auto; gap: 16px; align-items: center; margin: 12px 0 4px; }
@media (max-width: 720px) { .find-bar { grid-template-columns: 1fr; } }
#find .row-actions { text-align: left; }

/* ============ Sign-in ============ */
body:not(.signed-in) .topbar,
body:not(.signed-in) .tabs,
body:not(.signed-in) main { display: none; }
body.signed-in .gate { display: none; }

.gate {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: linear-gradient(140deg, #0b6e51 0%, #0e8a65 50%, #14a37a 100%);
}
.gate-box {
  width: min(400px, 100%);
  background: var(--card);
  border-radius: 18px;
  padding: 36px 30px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .25);
}
.gate-box h1 { font-size: 22px; margin: 14px 0 6px; }
.logo.big { width: 60px; height: 60px; font-size: 28px; margin: 0 auto; background: var(--brand); color: #fff; }
.gate-box .btn { margin-top: 10px; }

.user { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.user span { opacity: .9; }
.btn.light { background: rgba(255, 255, 255, .16); color: #fff; border-color: rgba(255, 255, 255, .35); }
.btn.light:hover { background: rgba(255, 255, 255, .26); }
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; }
  .user span { display: none; }
}

/* ============ Phones and small tablets ============ */
.logo { flex: none; }
.card-head { flex-wrap: wrap; }
.topbar { position: relative; padding-top: max(22px, env(safe-area-inset-top)); }
#toast { bottom: calc(28px + env(safe-area-inset-bottom)); }

@media (max-width: 720px) {
  body { font-size: 15px; }

  /* Header: name + sign out on top, chest number as a strip below */
  .topbar {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 12px;
    padding: max(14px, env(safe-area-inset-top)) 16px 14px;
  }
  .brand { gap: 10px; }
  .logo { width: 38px; height: 38px; font-size: 18px; border-radius: 10px; }
  .brand h1 { font-size: 18px; line-height: 1.2; }
  .brand p { font-size: 12px; }
  .user { grid-column: 2; grid-row: 1; align-self: start; }
  .chest {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    text-align: left;
    padding: 8px 14px;
  }
  .chest strong { font-size: 26px; }
  .spinner { position: absolute; right: 16px; bottom: 26px; width: 20px; height: 20px; }

  /* Tabs stay at the top while scrolling */
  .tabs { position: sticky; top: 0; z-index: 10; padding: 0 8px; }
  .tab { flex: 1; padding: 14px 8px; }

  main { padding: 16px 12px 40px; }
  .card { padding: 18px 16px; border-radius: 12px; margin-bottom: 16px; }
  h2 { font-size: 16px; margin-bottom: 14px; }

  /* 16px text stops iPhones zooming in when a box is tapped */
  input[type=text], input[type=number], input[type=password], select { font-size: 16px; height: 48px; }

  /* Full-width controls are easier to tap */
  .segmented { display: flex; width: 100%; }
  .segmented label { flex: 1; text-align: center; padding: 11px 6px; }
  .chips label { padding: 11px 14px; }
  .actions { margin-top: 18px; }
  .actions .btn { flex: 1 1 auto; }
  #register { width: 100%; }
  kbd { display: none; }
  .row-gap > * { flex: 1 1 100%; }
  .toolbar .btn { flex: 1; }

  /* Tables become a stack of cards, one per row */
  .table thead { display: none; }
  .table, .table tbody, .table tfoot, .table tr, .table td { display: block; width: 100%; }
  .table tr {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 8px 14px;
    margin-bottom: 10px;
    background: var(--card);
  }
  .table tbody tr:hover { background: var(--card); }
  .table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 14px;
    padding: 6px 0;
    border: 0;
    text-align: right;
    min-height: 32px;
  }
  .table td::before {
    content: attr(data-label);
    flex: none;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--muted);
    text-align: left;
  }
  .table td[data-label=""]::before { content: none; }
  .table td:empty { display: none; }
  .table td:first-child { font-size: 16px; }
  .table tfoot tr { background: var(--brand-soft); }
  .active-row, .table tr.active-row:hover { background: var(--brand-soft); }

  /* Buttons inside a card row wrap onto their own line */
  .row-actions { flex-wrap: wrap; justify-content: flex-start !important; white-space: normal; }
  .row-actions .btn { margin: 4px 6px 0 0; flex: 1 1 auto; }
  #m_table td:first-child { justify-content: flex-start; }
  .scroll-y { max-height: none; }

  .report-head h2 { font-size: 18px; }
  .modal-box { padding: 20px; }
  .gate-box { padding: 28px 20px; }
}

@media print {
  .table thead { display: table-header-group !important; }
  .table { display: table !important; }
  .table tbody { display: table-row-group !important; }
  .table tr { display: table-row !important; border: 0; padding: 0; margin: 0; }
  .table td { display: table-cell !important; text-align: left; }
  .table td::before { content: none !important; }
}
@media (max-width: 720px) {
  .table td .badge { flex: none; margin-left: 8px; }
  .table td > strong { margin-left: auto; }
}
