/* ============================================================
   RaceMaster — Main application styles
   ============================================================ */

:root {
  --sidebar-w: 200px;
  --header-h: 48px;

  /* Brand */
  --accent: #1a6e3c;
  --accent-light: #d4edda;
  --danger: #c0392b;
  --warn: #e67e22;
  --success: #1e8449;

  /* Surfaces */
  --bg: #f5f5f5;
  --panel: #ffffff;
  --panel-alt: #f0f0f0;

  /* Borders */
  --border: #ccc;
  --border-light: #eee;

  /* Text */
  --text: #222;
  --muted: #666;

  /* Header */
  --header-fg: #fff;
  --header-fg-mid: rgba(255,255,255,0.75);
  --header-fg-dim: rgba(255,255,255,0.6);
  --header-warn: rgba(255,220,50,0.95);

  /* Sidebar */
  --sidebar-bg: #2c3e50;
  --sidebar-fg: #ecf0f1;
  --sidebar-link: #bdc3c7;
  --sidebar-section: #7f8c8d;
  --sidebar-active: #2ecc71;
  --sidebar-hover-bg: rgba(255,255,255,0.08);
  --sidebar-active-bg: rgba(255,255,255,0.1);

  /* Buttons */
  --btn-secondary-bg: #ba5579;
  --btn-sm-bg: #7f8c8d;
  --btn-edit-bg: #2980b9;
  --btn-disabled-bg: #d5d8dc;
  --btn-disabled-fg: #8d9ea0;

  /* Tables */
  --table-header-bg: #ecf0f1;
  --table-hover: #f8f9fa;
  --row-error-bg: #fde8e8;
  --row-banned-fg: #a93226;
  --row-timing-bg: #fff3cd;
  --row-editing-bg: #cfe2ff;
  --row-sep: #d0d0d0;

  /* Badges / dataset list */
  --badge-private-bg: #e8e8e8;
  --badge-private-fg: #555;
  --df-own-bg: #d0e8fb;
  --df-own-hover: #b8d9f5;
  --df-other-bg: #fbd0d0;
  --df-other-hover: #f5b8b8;

  /* Status bar */
  --status-ok-bg: #d5f5e3;
  --status-error-bg: #fadbd8;

  /* Overlays */
  --overlay-bg: rgba(0,0,0,0.45);
  --overlay-busy: rgba(255,255,255,0.75);

  /* Shadows */
  --shadow-sm: rgba(0,0,0,0.15);
  --shadow-md: rgba(0,0,0,0.25);
  --shadow-lg: rgba(0,0,0,0.3);
}

/* ---- Reset ---- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body { font-family: system-ui, -apple-system, 'Segoe UI', sans-serif; font-size: 14px; color: var(--text); background: var(--bg); }

/* ---- Layout ---- */
.app-shell {
  display: grid;
  grid-template-rows: var(--header-h) 1fr;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  overflow: hidden;
}

.app-header {
  grid-column: 1 / -1;
  background: var(--accent);
  color: var(--header-fg);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
}

.app-header h1 { font-size: 1.1rem; font-weight: 600; }
.app-header .event-name { font-size: 0.9rem; opacity: 0.85; }
.app-header .app-icon { width: 28px; height: 28px; flex-shrink: 0; }
.app-header .btn { color: var(--header-fg); border-color: var(--header-fg); }
#header-username    { font-size: 0.8rem; color: var(--header-fg-mid); }
#header-server-status { font-size: 0.8rem; }

.btn-hamburger {
  display: none;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--header-fg);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
  flex-shrink: 0;
}

#nav-backdrop {
  display: none;
  position: fixed;
  inset: var(--header-h) 0 0 0;
  background: var(--overlay-bg);
  z-index: 899;
}

.app-sidebar {
  background: var(--sidebar-bg);
  color: var(--sidebar-fg);
  overflow-y: auto;
  padding: 8px 0;
  transition: transform 0.22s ease;
}

.app-sidebar nav a {
  display: block;
  padding: 8px 16px;
  color: var(--sidebar-link);
  text-decoration: none;
  font-size: 0.85rem;
  border-left: 3px solid transparent;
  transition: background 0.15s;
}

.app-sidebar nav a:hover { background: var(--sidebar-hover-bg); color: var(--header-fg); }
.app-sidebar nav a.active { color: var(--header-fg); border-left-color: var(--sidebar-active); background: var(--sidebar-active-bg); }
.app-sidebar nav .nav-section {
  padding: 12px 16px 4px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--sidebar-section);
}

.app-main {
  overflow-y: auto;
  padding: 16px;
  min-height: 0;
}

/* ---- Views ---- */
.view { display: block; }
.view[hidden] { display: none; }

.view-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}

.view-header h2 { font-size: 1.1rem; font-weight: 600; }

.btn-help {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  background: transparent;
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
}
.btn-help.active { background: var(--accent); color: #fff; }

.view-help {
  background: var(--accent-light);
  border-left: 3px solid var(--accent);
  border-radius: 0 4px 4px 0;
  padding: 10px 14px;
  margin-bottom: 14px;
  font-size: 0.88rem;
  line-height: 1.6;
}
.view-help p { margin: 0 0 6px; }
.view-help p:last-child { margin-bottom: 0; }
.view-help strong { color: var(--accent); }

/* ---- Cards ---- */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 16px;
  margin-bottom: 16px;
}
.card h3 { font-size: 0.9rem; font-weight: 600; margin-bottom: 12px; }
.card-warn { background: #fff8e1; border-color: #f9a825; }

/* ---- Home summary grid ---- */
.home-ev-table { border-collapse: collapse; margin-top: 2px; font-size: 0.85rem; }
.home-ev-label { color: var(--muted); padding-right: 12px; white-space: nowrap; vertical-align: top; }

.home-info-row {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 16px;
}
.home-stats-card,
.home-event-card    { flex: 0 0 auto; width: fit-content; margin-bottom: 0; }
.home-getting-started { flex: 1; min-width: 0; margin-bottom: 0; }
.home-stats-table { border-collapse: collapse; font-size: 0.85rem; }
.home-stats-table th { color: var(--muted); font-weight: 600; font-size: 0.78rem;
  text-align: left; padding: 0 10px 4px 0; white-space: nowrap; }
.home-stats-table th.num { text-align: center; }
.home-stats-table td { vertical-align: top; padding: 1px 10px 1px 0; white-space: nowrap; }
.home-stats-table td.num { text-align: center; padding-right: 10px; font-variant-numeric: tabular-nums; }
.home-stats-section { font-size: 0.78rem; font-weight: 600; color: var(--muted);
  padding-top: 10px; padding-bottom: 4px; }
.home-stats-section:first-child { padding-top: 0; }

/* ---- Event settings layout ---- */
.event-cards-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 16px;
}
.event-cards-row .card { flex: 1 1 220px; margin-bottom: 0; }

.event-footer-row { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.event-footer-row .card { margin-bottom: 0; flex: 0 0 auto; }
.event-footer-row .btn-row { margin-bottom: 0; }
#view-event .card { background: var(--accent-light); }


/* ---- Buttons ---- */
.btn[hidden] { display: none; }
.btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 7px 14px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  font-family: inherit;
  background: var(--accent);
  color: var(--header-fg);
  transition: opacity 0.15s;
}
.btn:hover { opacity: 0.88; }
.btn:disabled { background: var(--btn-disabled-bg) !important; color: var(--btn-disabled-fg) !important; cursor: not-allowed; }
.btn-secondary { background: var(--btn-secondary-bg); }
.btn-danger    { background: var(--danger); }
.btn-warn      { background: var(--warn); }
.btn-outline   { background: transparent; border: 1px solid var(--accent); color: var(--accent); }
.btn-active    { background: var(--accent); color: var(--header-fg); }

.btn-sm {
  padding: 3px 8px;
  font-size: 0.78rem;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  background: var(--btn-sm-bg);
  color: var(--header-fg);
}
.btn-sm:hover { opacity: 0.85; }
.btn-edit   { background: var(--btn-edit-bg); }
.btn-delete-entry, .btn-del-helper, .btn-delete { background: var(--danger); }
.btn-save   { background: var(--accent); }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }

/* ---- Forms ---- */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px 16px;
  margin-bottom: 12px;
  align-items: end;
}

.form-field { display: flex; flex-direction: column; gap: 3px; }
.form-field label { font-size: 0.78rem; color: var(--muted); font-weight: 500; }

input[type="text"],
input[type="date"],
input[type="number"],
input[type="time"],
select,
textarea {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.85rem;
  font-family: inherit;
  background: var(--panel);
  width: 100%;
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}

/* ---- Tables ---- */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.data-table th {
  background: var(--table-header-bg);
  text-align: left;
  padding: 6px 8px;
  border-bottom: 2px solid var(--border);
  font-weight: 600;
  white-space: nowrap;
  position: sticky;
  top: 0;
  z-index: 1;
}

.data-table td {
  padding: 5px 8px;
  border-bottom: 1px solid var(--border-light);
  vertical-align: middle;
}

.data-table tr:hover td { background: var(--table-hover); }
.row-error td  { background: var(--row-error-bg) !important; }
.row-banned td { background: var(--row-error-bg) !important; color: var(--row-banned-fg) !important; }
.row-timing-target td { background: var(--row-timing-bg) !important; }
.row-editing td       { background: var(--row-editing-bg) !important; }
.results-cat-separator td { padding: 0; border-bottom: 2px solid var(--border); }
.col-sep { border-left: 2px solid var(--border) !important; }
.data-table th[colspan] { text-align: center; }
.prizes-cat-sep td { padding: 1px 0; background: var(--row-sep) !important; border-bottom: none; }
#prizes-table { width: auto; }
#prizes-hint { flex-shrink: 0; padding: 4px 0; margin: 0; }
#view-results:not([hidden]),
#view-categories:not([hidden]) { display: flex; flex-direction: column; height: 100%; }
#results-sticky-top,
#cat-tab-bar { flex-shrink: 0; }
#view-results .tab-panel.active,
#view-categories .tab-panel.active { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }
#view-results .tab-panel > p,
#view-categories .tab-panel .btn-row { flex-shrink: 0; }
#view-results .table-scroll,
#view-categories .table-scroll { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
#view-categories thead tr:nth-child(2) th { top: 30px; }

/* ---- Two-column form+table layout ---- */
.split-layout {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 16px;
  align-items: start;
}

@media (max-width: 900px) {
  .split-layout { grid-template-columns: 1fr; }
  .split-layout > * { min-width: 0; }
}

/* ---- Modal ---- */
.modal-backdrop,
.choice-dialog-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal-backdrop[hidden] { display: none; }

.modal-box {
  background: var(--panel);
  border-radius: 8px;
  padding: 24px;
  width: 560px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 8px 40px var(--shadow-md);
}

.modal-box h2 { font-size: 1rem; margin-bottom: 16px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

/* ---- Data-file dataset list ---- */
.df-badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 10px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.df-badge-private { background: var(--badge-private-bg); color: var(--badge-private-fg); }
.df-badge-public  { background: var(--accent-light); color: var(--accent); }
#df-dataset-list .df-row-own td        { background: var(--df-own-bg); }
#df-dataset-list .df-row-own:hover td  { background: var(--df-own-hover); }
#df-dataset-list .df-row-other td      { background: var(--df-other-bg); }
#df-dataset-list .df-row-other:hover td{ background: var(--df-other-hover); }

/* ---- Status bar ---- */
#status-bar {
  position: fixed;
  bottom: 0;
  left: var(--sidebar-w);
  right: 0;
  padding: 4px 16px;
  font-size: 0.8rem;
  z-index: 500;
}

.status-ok    { background: var(--status-ok-bg); color: var(--success); }
.status-error { background: var(--status-error-bg); color: var(--danger); }

/* ---- Busy overlay ---- */
#busy-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-busy);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  flex-direction: column;
  gap: 12px;
}

#busy-overlay .spinner {
  width: 40px; height: 40px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Tabs ---- */
.tab-bar {
  display: flex;
  border-bottom: 2px solid var(--border);
  margin-bottom: 16px;
}

.tab-bar button {
  padding: 8px 16px;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 0.85rem;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
}

.tab-bar button.active {
  border-bottom-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ---- Scrollable table containers ---- */
.table-scroll {
  overflow-x: auto;
  overflow-y: auto;
  margin-bottom: 12px;
}

/* ---- Misc ---- */
.count-badge {
  display: inline-block;
  background: var(--accent);
  color: var(--header-fg);
  border-radius: 10px;
  padding: 1px 8px;
  font-size: 0.75rem;
  font-weight: 600;
  margin-left: 4px;
}

.text-muted { color: var(--muted); }
.text-sm    { font-size: 0.78rem; }
.mb-8       { margin-bottom: 8px; }

/* ---- Entries view: inline status message ---- */
.entry-status {
  font-size: 0.95rem;
  font-weight: 600;
  padding: 5px 14px;
  border-radius: 4px;
  margin-left: 4px;
}
/* ---- Entries / Helpers / Finishers view: flex column, table fills remaining space ---- */
.view-split:not([hidden])              { display: flex; flex-direction: column; height: 100%; }
.view-split .view-header               { flex-shrink: 0; }
.view-split .card                      { background: var(--accent-light); }
.view-split .split-layout              { flex: 1 1 auto; min-height: 0; grid-template-rows: 1fr; }
.view-split .table-panel               { display: flex; flex-direction: column; align-self: stretch; min-height: 0; }
.view-split .table-panel .table-scroll { flex: 1 1 auto; overflow-y: auto; min-height: 0; }

/* ---- SI Results view: flex column, table fills remaining space ---- */
#view-si-results:not([hidden]) {
  display: flex;
  flex-direction: column;
  height: 100%;
}
#view-si-results .view-header,
#view-si-results .card {
  flex-shrink: 0;
}
#view-si-results .table-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: auto;
  overflow-y: auto;
}
#view-si-results .data-table {
  width: max-content;
  min-width: 100%;
}

/* ---- Pre-entries view: flex column, card fixed, table fills remaining space ---- */
#view-pre-entries:not([hidden]) {
  display: flex;
  flex-direction: column;
  height: 100%;
}
#view-pre-entries .view-header,
#view-pre-entries .card {
  flex-shrink: 0;
}
#view-pre-entries .table-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: auto;
  overflow-y: auto;
}
#view-pre-entries .data-table {
  width: max-content;
  min-width: 100%;
}

/* ---- Simple list views: flex column, table fills remaining space ---- */
.view-list:not([hidden]) { display: flex; flex-direction: column; height: 100%; }
.view-list .view-header,
.view-list .btn-row,
.view-list .card         { flex-shrink: 0; }
.view-list .table-scroll { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
/* tab panels inside view-list must also stretch so their table-scroll can scroll */
.view-list .tab-panel.active { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }
/* duplicates panels replace the main table; flex layout only when visible */
#people-dupes-panel:not([hidden]),
#clubs-dupes-panel:not([hidden]) { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }
/* duplicate A/B column colouring */
.dupe-a { background: #dbeafe !important; }
.dupe-b { background: #fef9c3 !important; }
.btn-dupe-a { background: #93c5fd; color: #1e3a5f; border: none; border-radius: 3px; padding: 3px 8px; font-size: 0.78rem; cursor: pointer; }
.btn-dupe-a:hover { opacity: 0.85; }
.btn-dupe-b { background: #fde68a; color: #5f4a00; border: none; border-radius: 3px; padding: 3px 8px; font-size: 0.78rem; cursor: pointer; }
.btn-dupe-b:hover { opacity: 0.85; }

/* narrow: form takes natural height, table fills the rest */
@media (max-width: 900px) {
  .view-split .split-layout { grid-template-rows: auto 1fr; }
}

@media (max-width: 640px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  /* Without min-width:0, the header's flex min-content inflates the 1fr track
     beyond the viewport — app-main ends up wider than the viewport and its
     scrollbar lands off-screen, making content appear clipped with no scroll. */
  .app-header { min-width: 0; overflow: hidden; }

  .btn-hamburger { display: flex; }

  .app-sidebar {
    position: fixed;
    top: var(--header-h);
    left: 0;
    bottom: 0;
    width: var(--sidebar-w);
    transform: translateX(-100%);
    z-index: 900;
  }

  .app-shell.nav-open .app-sidebar {
    transform: translateX(0);
  }

  .app-shell.nav-open #nav-backdrop {
    display: block;
  }

  #status-bar { left: 0; }

  /* Pin .app-main to its 1fr grid track so it can't inflate past the viewport */
  .app-main { min-width: 0; }

  /* Let view headers wrap rather than overflow horizontally */
  .view-header { flex-wrap: wrap; }
}

@media print { .app-shell { display: block; } .app-sidebar, .app-header, #status-bar { display: none; } }

/* ---- Name typeahead dropdown ---- */
.name-typeahead {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 4px 4px;
  list-style: none;
  z-index: 100;
  max-height: 200px;
  overflow-y: auto;
  box-shadow: 0 4px 12px var(--shadow-sm);
}
.name-typeahead li {
  padding: 6px 8px;
  cursor: pointer;
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.name-typeahead li:hover,
.name-typeahead li:focus { background: var(--accent-light); outline: none; }

/* Choice dialog */
.choice-dialog-box {
  background: var(--panel);
  border-radius: 6px;
  padding: 20px 24px;
  max-width: 340px;
  width: 90%;
  box-shadow: 0 4px 24px var(--shadow-lg);
}
.choice-dialog-msg {
  margin: 0 0 16px;
  font-size: 0.95rem;
  color: var(--text);
}
.choice-dialog-btns {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-start;
}
.choice-dialog-cancel { margin-left: auto; }
.choice-dialog-box--wide { max-width: 480px; }
.choice-dialog-btns--vertical {
  flex-direction: column;
  align-items: stretch;
}
.choice-dialog-btns--vertical .choice-dialog-cancel {
  margin-left: 0;
  align-self: flex-end;
  width: auto;
}
.input-dialog-field {
  width: 100%;
  box-sizing: border-box;
  padding: 6px 8px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.95rem;
}