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

:root {
  --sidebar-w: 200px;
  --header-h: 48px;
  --accent: #1a6e3c;
  --accent-light: #d4edda;
  --danger: #c0392b;
  --warn: #e67e22;
  --bg: #f5f5f5;
  --panel: #ffffff;
  --panel-alt: #f0f0f0;
  --border: #ccc;
  --text: #222;
  --muted: #666;
  --header-fg: #fff;
}

/* ---- 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: #fff;
  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); }

.btn-hamburger {
  display: none;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: #fff;
  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: rgba(0,0,0,0.4);
  z-index: 899;
}

.app-sidebar {
  background: #2c3e50;
  color: #ecf0f1;
  overflow-y: auto;
  padding: 8px 0;
  transition: transform 0.22s ease;
}

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

.app-sidebar nav a:hover { background: rgba(255,255,255,0.08); color: #fff; }
.app-sidebar nav a.active { color: #fff; border-left-color: #2ecc71; background: rgba(255,255,255,0.1); }
.app-sidebar nav .nav-section {
  padding: 12px 16px 4px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #7f8c8d;
}

.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; }

/* ---- 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; }

/* ---- 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: #fff;
  transition: opacity 0.15s;
}
.btn:hover { opacity: 0.88; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-secondary { background: #95a5a6; }
.btn-danger    { background: var(--danger); }
.btn-warn      { background: var(--warn); }
.btn-outline   { background: transparent; border: 1px solid var(--accent); color: var(--accent); }

.btn-sm {
  padding: 3px 8px;
  font-size: 0.78rem;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  background: #7f8c8d;
  color: #fff;
}
.btn-sm:hover { opacity: 0.85; }
.btn-edit   { background: #2980b9; }
.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: #fff;
  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: #ecf0f1;
  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 #eee;
  vertical-align: middle;
}

.data-table tr:hover td { background: #f8f9fa; }
.row-error td  { background: #fde8e8 !important; }
.row-banned td { background: #fde8e8 !important; color: #a93226 !important; }
.row-timing-target td { background: #fff3cd !important; }
.row-editing td       { background: #cfe2ff !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: #d0d0d0 !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: rgba(0,0,0,0.45);
  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 rgba(0,0,0,0.25);
}

.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: #e8e8e8; color: #555; }
.df-badge-public  { background: #d4edda; color: #1a6e3c; }
#df-dataset-list .df-row-own td        { background: #d0e8fb; }
#df-dataset-list .df-row-own:hover td  { background: #b8d9f5; }
#df-dataset-list .df-row-other td      { background: #fbd0d0; }
#df-dataset-list .df-row-other:hover td{ background: #f5b8b8; }

/* ---- 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: #d5f5e3; color: #1e8449; }
.status-error { background: #fadbd8; color: #c0392b; }

/* ---- Busy overlay ---- */
#busy-overlay {
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,0.75);
  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 #ccc;
  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: #fff;
  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%;
}

/* ---- 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      { flex-shrink: 0; }
.view-list .table-scroll { flex: 1 1 auto; min-height: 0; overflow-y: auto; }

/* 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 rgba(0,0,0,0.15);
}
.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: #fff;
  border-radius: 6px;
  padding: 20px 24px;
  max-width: 340px;
  width: 90%;
  box-shadow: 0 4px 24px rgba(0,0,0,0.3);
}
.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-end;
}