@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap');

:root{
  /* --- spacing + shape tokens --- */
  --radius: 14px;
  --radius-sm: 10px;
  --radius-lg: 18px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  /* --- typography --- */
  --font: "Plus Jakarta Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, Menlo, Consolas, monospace;
  --text-sm: 13px;
  --text: 15px;
  --text-lg: 19px;

  /* --- motion --- */
  --ease: cubic-bezier(.2,.8,.2,1);
  --dur: 140ms;

  /* --- default colors (dark theme fallback) --- */
  --bg: #0b1020;
  --surface: #121a33;
  --surface-2: #0c142b;
  --muted: #92a0c6;
  --border: rgba(146, 160, 198, 0.22);
  --shadow: 0 12px 36px rgba(0,0,0,0.35);
  --accent: #6aa9ff;
  --focus: rgba(106, 169, 255, 0.35);
  --success: #35d07f;
  --warning: #f59e0b;
  --danger: #ff6b6b;
  --info: #38bdf8;
}

:root[data-theme="light"]{
  --bg: #f7f8fb;
  --surface: #ffffff;
  --surface-2: #f2f4f8;
  --text: #0f172a;
  --muted: #64748b;
  --border: rgba(15, 23, 42, 0.12);
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);

  --accent: #2563eb;
  --focus: rgba(37, 99, 235, 0.35);

  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --info: #0ea5e9;
}

:root[data-theme="dark"]{
  --bg: #0b1020;
  --surface: #121a33;
  --surface-2: #0c142b;
  --text: #e6ecff;
  --muted: #92a0c6;
  --border: rgba(146, 160, 198, 0.22);
  --shadow: 0 12px 36px rgba(0,0,0,0.35);

  --accent: #6aa9ff;
  --focus: rgba(106, 169, 255, 0.35);

  --success: #35d07f;
  --warning: #f59e0b;
  --danger: #ff6b6b;
  --info: #38bdf8;
}

*{ box-sizing: border-box; }

html, body{ height: 100%; }

body{
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--text);
  line-height: 1.6;
}

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

code, .mono{ font-family: var(--mono); }

:focus-visible{
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 8px;
}

@media (prefers-reduced-motion: reduce){
  *{ transition: none !important; scroll-behavior: auto !important; }
}

/* --- shell: sidebar-ready (placeholder only) --- */
.app-shell{
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1fr;
}

@media (min-width: 1024px){
  .app-shell{
    /* Icon rail sidebar (single main icon for now) */
    grid-template-columns: 72px 1fr;
  }
}

.sidebar-placeholder{
  display: none;
  border-right: 1px solid var(--border);
  background: color-mix(in oklab, var(--surface) 90%, var(--bg));
  padding: var(--space-4) var(--space-2);
}
@media (min-width: 1024px){
  .sidebar-placeholder{ display: block; }
}
.sidebar-placeholder .hint{
  color: var(--muted);
  font-size: var(--text-sm);
  line-height: 1.4;
}

.sidebar-nav{
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
}

.side-main{
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.side-main:hover{
  background: color-mix(in oklab, var(--surface) 80%, var(--surface-2));
}
.side-main:active{
  transform: translateY(0.5px);
}
.side-main svg{
  width: 22px;
  height: 22px;
  display: block;
}

.sr-only{
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

.app-main{ min-width: 0; }

.topbar{
  position: sticky;
  top: 0;
  z-index: 20;
  border-bottom: 1px solid var(--border);
  background: color-mix(in oklab, var(--surface) 88%, transparent);
  backdrop-filter: blur(8px);
}

.container{
  max-width: 1120px;
  margin: 0 auto;
  padding: 0 var(--space-4);
}

.container--wide{
  max-width: 1480px;
}

.topbar-inner{
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4) 0;
}

.title{
  display: grid;
  gap: 2px;
}
.title h1{
  margin: 0;
  font-size: var(--text-lg);
  letter-spacing: 0.2px;
}
.sub{
  color: var(--muted);
  font-size: var(--text-sm);
}

.note{
  color: var(--muted);
  font-size: var(--text-sm);
}

.topbar-actions{
  display: flex;
  gap: var(--space-2);
  align-items: center;
  flex-wrap: wrap;
}

.btn{
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 8px 10px;
  border-radius: 999px;
  font: 600 var(--text-sm)/1 var(--font);
  cursor: pointer;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.btn:hover{ background: color-mix(in oklab, var(--surface) 80%, var(--surface-2)); }
.btn:active{ transform: translateY(0.5px); }
.btn:disabled{
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}
.btn.link{
  border-color: transparent;
  background: transparent;
  padding: 8px 10px;
}

.btn-primary{
  border-color: color-mix(in oklab, var(--accent) 35%, var(--border));
  background: color-mix(in oklab, var(--accent) 12%, var(--surface));
}
.btn-primary:hover{
  background: color-mix(in oklab, var(--accent) 16%, var(--surface));
}

.btn-secondary{
  background: var(--surface);
}

.page{
  padding: var(--space-5) 0 var(--space-6);
}

.stack{
  display: grid;
  gap: var(--space-4);
}

.narrow{
  max-width: 520px;
  margin: 0 auto;
}

.card{
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}

.card-pad{
  padding: var(--space-4);
}

.card-head{
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
}

/* --- forms --- */
.form{
  display: grid;
  gap: var(--space-3);
}

.field{
  display: grid;
  gap: 6px;
  font-size: var(--text-sm);
  color: var(--muted);
}

.field .key{
  font-family: var(--mono);
  color: color-mix(in oklab, var(--accent) 65%, var(--text));
  font-size: var(--text-sm);
}

input, select{
  width: 100%;
  padding: 10px 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: 500 var(--text)/1.2 var(--font);
  outline: none;
}

input:focus, select:focus{
  border-color: color-mix(in oklab, var(--accent) 55%, var(--border));
  box-shadow: 0 0 0 3px var(--focus);
}

.form-grid{
  display: grid;
  gap: var(--space-3);
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
}

.section-title{
  margin: var(--space-4) 0 var(--space-2);
  font-weight: 800;
  color: var(--text);
}

.divider{
  height: 1px;
  background: var(--border);
  margin: var(--space-3) 0;
}

.error{
  color: var(--danger);
  font-size: var(--text-sm);
  margin-top: 4px;
}

.success{
  color: var(--success);
  font-weight: 700;
  font-size: var(--text-sm);
}

/* --- logs box --- */
.log-box{
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px;
  border-radius: var(--radius);
  height: 300px;
  overflow: auto;
  font-size: 12px;
  line-height: 1.5;
}

/* --- table --- */
.table-card{
  margin-top: var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
}

.table-scroll{
  max-height: 70vh;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

table{
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--text-sm);
  /* table-layout: fixed; -- removed to allow content-based column sizing */
}

thead th{
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface);
  color: color-mix(in oklab, var(--muted) 82%, var(--text));
  text-align: left;
  padding: 12px 12px;
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  letter-spacing: 0.2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

tbody td{
  padding: 12px 12px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  vertical-align: top;
  overflow: hidden;
  text-overflow: ellipsis;
}

tbody tr:hover td{
  background: color-mix(in oklab, var(--surface-2) 60%, transparent);
}

.right{ text-align: right; }

/* Column sizing: horse (winner) expands to fill space; status/result fit content */
.col-venue{ width: clamp(90px, 12vw, 140px); }
.col-time{ width: clamp(70px, 10vw, 100px); }
.col-winner{ width: auto; min-width: 120px; }
.col-bsp{ width: 80px; }
.col-status{ width: 1px; white-space: nowrap; }
.col-result{ width: 1px; white-space: nowrap; }

/* Prefer wrapping for winner name; keep other columns tidy. */
td:nth-child(3), th:nth-child(3){
  white-space: normal;
  overflow: visible;
  text-overflow: unset;
}

/* optional: keep first column readable while scrolling horizontally */
@media (max-width: 900px){
  thead th:first-child,
  tbody td:first-child{
    position: sticky;
    left: 0;
    z-index: 1;
    background: var(--surface);
    box-shadow: 1px 0 0 var(--border);
  }
  thead th:first-child{ z-index: 3; }
}

/* --- pills / badges --- */
.pill{
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: color-mix(in oklab, var(--surface-2) 65%, var(--surface));
  color: color-mix(in oklab, var(--muted) 88%, var(--text));
  white-space: nowrap;
}
.pill::before{
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--muted);
}

.pill.no-dot::before{ display:none; }

/* Filter toggle switch */
.filter-toggle{
  cursor: pointer;
  user-select: none;
  gap: 10px;
}
.filter-toggle input{
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle-switch{
  width: 36px;
  height: 20px;
  background: var(--muted);
  border-radius: 10px;
  position: relative;
  transition: background 0.2s ease;
}
.toggle-switch::after{
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: var(--surface);
  border-radius: 50%;
  transition: transform 0.2s ease;
}
.filter-toggle input:checked + .toggle-switch{
  background: var(--success);
}
.filter-toggle input:checked + .toggle-switch::after{
  transform: translateX(16px);
}
.filter-toggle:has(input:checked){
  color: color-mix(in oklab, var(--info) 70%, var(--text));
  border-color: color-mix(in oklab, var(--info) 24%, var(--border));
}

.pill--success{ color: color-mix(in oklab, var(--success) 72%, var(--text)); border-color: color-mix(in oklab, var(--success) 22%, var(--border)); }
.pill--success::before{ background: var(--success); }
.pill--warning{ color: color-mix(in oklab, var(--warning) 75%, var(--text)); border-color: color-mix(in oklab, var(--warning) 24%, var(--border)); }
.pill--warning::before{ background: var(--warning); }
.pill--danger{ color: color-mix(in oklab, var(--danger) 75%, var(--text)); border-color: color-mix(in oklab, var(--danger) 24%, var(--border)); }
.pill--danger::before{ background: var(--danger); }
.pill--info{ color: color-mix(in oklab, var(--info) 70%, var(--text)); border-color: color-mix(in oklab, var(--info) 24%, var(--border)); }
.pill--info::before{ background: var(--info); }
.pill--neutral{ border-color: var(--border); }
.pill--void{ color: var(--muted); border-color: var(--border); font-style: italic; }
.pill--void::before{ background: var(--muted); opacity: 0.5; }

.muted{ color: var(--muted); }

/* hide-only helper kept for existing markup */
@media (max-width: 720px){
  .hide-sm{ display: none; }
  /* Keep table usable: shrink columns first, then allow horizontal scroll if still needed. */
  table{ min-width: 0; }
}

/* subtle scrollbars (container only) */
.table-scroll{
  scrollbar-width: thin;
  scrollbar-color: color-mix(in oklab, var(--muted) 35%, transparent) transparent;
}
.table-scroll::-webkit-scrollbar{ height: 10px; width: 10px; }
.table-scroll::-webkit-scrollbar-thumb{
  background: color-mix(in oklab, var(--muted) 28%, transparent);
  border-radius: 999px;
  border: 3px solid transparent;
  background-clip: padding-box;
}
.table-scroll::-webkit-scrollbar-track{ background: transparent; }

/* --- New row animations --- */
/* Note: animations must be on td cells, not tr, because table rows
   don't properly support transform/box-shadow in most browsers */

@keyframes cell-glow {
  0% {
    background-color: rgba(100, 200, 255, 0.5) !important;
  }
  50% {
    background-color: rgba(100, 200, 255, 0.3) !important;
  }
  100% {
    background-color: transparent !important;
  }
}

tbody tr.row-new td {
  animation: cell-glow 2s ease-out forwards !important;
}

/* --- stats panel --- */
.stats-panel {
  margin-bottom: var(--space-4);
}

.stats-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stats-left {
  font-weight: 600;
  color: var(--text);
}

.stats-right {
  font-weight: 700;
  font-family: var(--mono);
}

.pnl-positive { color: var(--success); }
.pnl-negative { color: var(--danger); }

