/* ================================================================
   Raceup Hub — Main Stylesheet
   Shared across all pages. Based on home_final.html design tokens.
   ================================================================ */

:root {
  --red: #EE4439;
  --red-dark: #C4342A;
  --green: #1D9E75;
  --blue: #185FA5;
  --yellow: #EF9F27;
  --purple: #534AB7;
  --dark: #1A1A1A;
  --gray-100: #F8F9FA;
  --gray-200: #F0F1F3;
  --gray-300: #E0E2E6;
  --gray-400: #C4C7CC;
  --gray-500: #8E939B;
  --gray-700: #4A4F57;
  --white: #FFFFFF;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,.08);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }
body {
  font-family: 'Kanit', 'Sarabun', sans-serif;
  background: var(--gray-100);
  color: var(--dark);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
img { max-width: 100%; display: block; }

/* ===== TOP NAV ===== */
.topnav {
  position: sticky; top: 0; z-index: 100;
  background: var(--white);
  border-bottom: 1px solid var(--gray-300);
  box-shadow: var(--shadow);
}
.topnav-inner {
  max-width: 1200px; margin: 0 auto; padding: 0 32px;
  display: flex; align-items: center; justify-content: space-between;
  height: 56px;
}
.topnav-brand {
  display: flex; align-items: center; gap: 10px;
  font-weight: 700; font-size: 16px;
}
.topnav-brand .dot {
  width: 28px; height: 28px; border-radius: 6px;
  background: var(--red); color: #fff;
  display: grid; place-items: center;
  font-weight: 800; font-size: 13px;
}
.topnav-links { display: flex; gap: 2px; align-items: center; }
.topnav-link {
  padding: 8px 12px; border-radius: 6px;
  font-size: 13px; font-weight: 500;
  color: var(--gray-700);
  transition: all .2s;
  display: inline-flex; align-items: center; gap: 6px;
}
.topnav-link svg { stroke-width: 2; opacity: .85; }
.topnav-link:hover { background: var(--gray-200); color: var(--dark); }
.topnav-link.active { background: var(--red); color: #fff; }
.topnav-link.active svg { opacity: 1; }
.topnav-caret { font-size: 9px; opacity: .6; margin-left: 2px; }

/* Submenu (dropdown) — hover-intent friendly with invisible bridge */
.topnav-group { position: relative; }
.topnav-submenu {
  position: absolute; top: 100%; left: 0;
  background: #fff; min-width: 200px;
  border: 1px solid var(--gray-300); border-radius: 8px;
  box-shadow: var(--shadow-md);
  padding: 6px; margin-top: 0;
  display: flex; flex-direction: column; gap: 2px;
  z-index: 110;
  /* hover-intent: hidden by default, shown via .open class (JS) */
  visibility: hidden;
  opacity: 0;
  transform: translateY(4px);
  pointer-events: none;
  transition: opacity .15s ease, transform .15s ease, visibility 0s linear .15s;
}
/* Invisible bridge — covers the gap between link and submenu so mouse doesn't fall off */
.topnav-submenu::before {
  content: '';
  position: absolute;
  top: -10px; left: 0; right: 0;
  height: 10px;
  background: transparent;
}
.topnav-group.open .topnav-submenu,
.topnav-group:focus-within .topnav-submenu {
  visibility: visible;
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
  transition: opacity .15s ease, transform .15s ease, visibility 0s;
}
.topnav-sub-link {
  padding: 7px 12px; border-radius: 5px;
  font-size: 12.5px; font-weight: 500;
  color: var(--gray-700); white-space: nowrap;
  transition: all .15s;
  display: flex; align-items: center; gap: 8px;
}
.topnav-sub-link svg { stroke-width: 2; opacity: .7; width: 13px; height: 13px; flex-shrink: 0; }
.topnav-sub-link:hover { background: var(--gray-100); color: var(--red); }
.topnav-sub-link:hover svg { opacity: 1; }

/* ── Level-2 submenu (sub-sub group flyout) ── */
.topnav-subgroup { position: relative; }
.topnav-sub-link.has-children { cursor: default; color: var(--gray-500); font-weight: 600; }
.topnav-sub-link.has-children .sg-caret { margin-left: auto; opacity: .55; font-size: 10px; }
.topnav-sub-link.has-children:hover { background: var(--gray-100); color: var(--dark); }
.topnav-submenu-2 {
  position: absolute; top: -7px; left: 100%;
  background: #fff; min-width: 190px;
  border: 1px solid var(--gray-300); border-radius: 8px;
  box-shadow: var(--shadow-md);
  padding: 6px; margin-left: 5px;
  display: flex; flex-direction: column; gap: 2px;
  z-index: 120;
  visibility: hidden; opacity: 0; transform: translateX(4px);
  pointer-events: none;
  transition: opacity .15s ease, transform .15s ease, visibility 0s linear .15s;
}
/* bridge so the mouse can cross the gap into the flyout */
.topnav-submenu-2::before { content: ''; position: absolute; top: 0; bottom: 0; left: -9px; width: 9px; background: transparent; }
.topnav-subgroup:hover > .topnav-submenu-2,
.topnav-subgroup:focus-within > .topnav-submenu-2 {
  visibility: visible; opacity: 1; transform: translateX(0); pointer-events: auto;
  transition: opacity .15s ease, transform .15s ease, visibility 0s;
}
.topnav-user {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; color: var(--gray-700);
}
.topnav-user .avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: linear-gradient(135deg, var(--red), var(--purple));
  color: #fff; display: grid; place-items: center;
  font-weight: 700; font-size: 12px;
}

/* Mobile nav */
.menu-toggle { display: none; background: none; border: none; cursor: pointer; font-size: 22px; }
@media (max-width: 900px) {
  .topnav-links { display: none; position: absolute; top: 56px; left: 0; right: 0;
    background: #fff; flex-direction: column; padding: 8px 16px;
    border-bottom: 1px solid var(--gray-300); box-shadow: var(--shadow-md); }
  .topnav-links.open { display: flex; }
  .topnav-link { width: 100%; }
  .menu-toggle { display: block; }
}

/* ===== HERO (Home page only — legacy) ===== */
.hero {
  background: linear-gradient(135deg, var(--red) 0%, var(--red-dark) 100%);
  color: var(--white);
  padding: 40px 0 32px;
}
.hero-inner { max-width: 1200px; margin: 0 auto; padding: 0 32px; }
.hero h1 { font-size: 26px; font-weight: 700; margin-bottom: 4px; }
.hero p { font-size: 14px; font-weight: 300; opacity: .85; }

/* ===== PAGE HERO — Unified across all section pages ===== */
/* Usage: <div class="page-hero ph-{section}">
            <div class="page-hero-inner">
              <div class="page-hero-icon"><i data-lucide="..."></i></div>
              <div class="page-hero-text">
                <h1>Title</h1>
                <p>Subtitle</p>
              </div>
              <div class="page-hero-actions">
                <a href="/">← Home</a>
              </div>
            </div>
          </div>
*/
.page-hero {
  color: var(--white);
  padding: 22px 0;
  background: linear-gradient(135deg, var(--ph-color, var(--red)) 0%, var(--ph-color-dark, var(--red-dark)) 100%);
}
.page-hero-inner {
  max-width: 1200px; margin: 0 auto; padding: 0 32px;
  display: flex; align-items: center; gap: 16px;
}
.page-hero-icon {
  width: 44px; height: 44px; border-radius: 12px;
  background: rgba(255,255,255,.18);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.page-hero-icon svg, .page-hero-icon i[data-lucide] {
  width: 24px; height: 24px; stroke-width: 2; stroke: #fff;
}
.page-hero-text { flex: 1; min-width: 0; }
.page-hero-text h1 { font-size: 22px; font-weight: 700; margin: 0 0 2px 0; line-height: 1.2; }
.page-hero-text p { font-size: 13px; font-weight: 300; opacity: .9; line-height: 1.4; margin: 0; }
.page-hero-actions { display: flex; gap: 8px; flex-shrink: 0; }
.page-hero-actions a,
.page-hero-actions button {
  display: inline-flex; align-items: center; gap: 6px;
  background: rgba(255,255,255,.18); color: #fff; text-decoration: none;
  padding: 7px 14px; border-radius: 8px; font-size: 12.5px; font-weight: 500;
  border: 1px solid rgba(255,255,255,.3); transition: all .15s;
  font-family: inherit; cursor: pointer; white-space: nowrap;
}
.page-hero-actions a:hover,
.page-hero-actions button:hover { background: rgba(255,255,255,.3); }

/* Section color variants (matches top-nav sections) */
.page-hero.ph-home     { --ph-color: #EE4439; --ph-color-dark: #B91C1C; }
.page-hero.ph-calendar { --ph-color: #0EA5E9; --ph-color-dark: #0369A1; }
.page-hero.ph-projects { --ph-color: #7C3AED; --ph-color-dark: #5B21B6; }
.page-hero.ph-info     { --ph-color: #0D9488; --ph-color-dark: #115E59; }
.page-hero.ph-stat     { --ph-color: #4F46E5; --ph-color-dark: #3730A3; }
.page-hero.ph-media    { --ph-color: #2563EB; --ph-color-dark: #1E40AF; }
.page-hero.ph-finance  { --ph-color: #059669; --ph-color-dark: #047857; }
.page-hero.ph-hr       { --ph-color: #EA580C; --ph-color-dark: #C2410C; }
.page-hero.ph-tools    { --ph-color: #475569; --ph-color-dark: #334155; }
.page-hero.ph-library  { --ph-color: #92400E; --ph-color-dark: #78350F; }

@media (max-width: 700px) {
  .page-hero { padding: 16px 0; }
  .page-hero-inner { flex-wrap: wrap; padding: 0 16px; gap: 12px; }
  .page-hero-text h1 { font-size: 18px; }
  .page-hero-actions { width: 100%; }
}

/* ===== MAIN ===== */
.main { max-width: 1200px; margin: 0 auto; padding: 32px; }

.section-title {
  font-size: 16px; font-weight: 600; color: var(--dark);
  margin: 24px 0 12px; display: flex; align-items: center; gap: 8px;
}
.section-title .badge {
  font-size: 11px; font-weight: 500; color: var(--gray-500);
  background: var(--gray-200); padding: 2px 8px; border-radius: 10px;
}

/* ===== CARD ===== */
.card {
  background: #fff; border-radius: var(--radius);
  padding: 20px; box-shadow: var(--shadow);
  border: 1px solid var(--gray-200);
  transition: all .2s;
}
.card:hover { box-shadow: var(--shadow-md); }

/* ===== QUICK LINKS GRID ===== */
.quick-links {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px;
  margin-bottom: 24px;
}
@media (max-width: 900px) { .quick-links { grid-template-columns: repeat(2, 1fr); } }
.ql-card {
  background: #fff; padding: 16px; border-radius: var(--radius);
  border: 1px solid var(--gray-200); text-align: center;
  transition: all .2s; display: block;
}
.ql-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); border-color: var(--red); }
.ql-icon {
  width: 44px; height: 44px; border-radius: 10px;
  margin: 0 auto 10px; display: grid; place-items: center;
  font-size: 22px;
}
.ql-name { font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.ql-desc { font-size: 11px; color: var(--gray-500); }

/* ===== BUTTON ===== */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px; border-radius: 6px;
  font-size: 13px; font-weight: 500;
  border: 1px solid var(--gray-300); background: #fff;
  cursor: pointer; transition: all .2s;
  font-family: inherit; color: var(--dark);
}
.btn:hover { background: var(--gray-100); border-color: var(--gray-400); }
.btn-primary { background: var(--red); color: #fff; border-color: var(--red); }
.btn-primary:hover { background: var(--red-dark); border-color: var(--red-dark); }

/* ===== TABLE ===== */
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { padding: 10px 12px; text-align: left; }
.table th {
  font-weight: 600; font-size: 12px; color: var(--gray-700);
  background: var(--gray-100); border-bottom: 1px solid var(--gray-300);
}
.table tbody tr { border-bottom: 1px solid var(--gray-200); }
.table tbody tr:hover { background: var(--gray-100); }

/* ===== STATUS BADGE ===== */
.status { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 500; }
.status-done { background: rgba(142,147,155,.15); color: var(--gray-700); }
.status-active { background: rgba(238,68,57,.12); color: var(--red); }
.status-prep { background: rgba(239,159,39,.15); color: var(--yellow); }
.status-upcoming { background: rgba(24,95,165,.12); color: var(--blue); }

/* ===== LOADING / ERROR ===== */
.loading {
  text-align: center; padding: 60px 20px; color: var(--gray-500);
}
.loading::before {
  content: ''; display: inline-block;
  width: 24px; height: 24px;
  border: 3px solid var(--gray-300); border-top-color: var(--red);
  border-radius: 50%; margin-right: 8px;
  vertical-align: middle;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-banner {
  background: rgba(238,68,57,.08); border: 1px solid rgba(238,68,57,.25);
  color: var(--red-dark); padding: 12px 16px; border-radius: 8px;
  font-size: 13px; margin: 16px 0;
}

/* ===== EVENT BADGES (Race results) ===== */
.ev-badge { display: inline-block; padding: 2px 8px; border-radius: 8px; font-size: 10px; font-weight: 600; }
/* Event CI colors — matches palette used across all pages */
.ev-psmh    { background: #95d5e7; color: #0F2A36; }
.ev-csmh    { background: #7444f5; color: #fff; }
.ev-rsmh    { background: #027bf7; color: #fff; }
.ev-ksmh    { background: #53c6dd; color: #0F2A36; }
.ev-csm     { background: #7444f5; color: #fff; }
.ev-nsmh    { background: #ef6361; color: #fff; }
.ev-cmsh    { background: #ffdf4d; color: #3D2F00; }
.ev-krm     { background: #eb552c; color: #fff; }
.ev-kym     { background: #ffc418; color: #3D2F00; }
.ev-pocari  { background: #007dc4; color: #fff; }
.ev-garmin  { background: #6dcef5; color: #0F2A36; }
.ev-allianz { background: #00468b; color: #fff; }
.ev-lg10k   { background: #1D9E75; color: #fff; }
.ev-amino   { background: #171c4b; color: #fff; }
.ev-ssp     { background: #E4002B; color: #fff; }

.flag { font-size: 10px; font-weight: 600; color: var(--gray-500); }

/* Race table — modern light theme */
.race-table { width: 100%; border-collapse: collapse; font-size: 12.5px; table-layout: auto; }
.race-table thead { position: sticky; top: 0; z-index: 2; }
.race-table th {
  background: var(--gray-100); color: var(--gray-700); padding: 10px 8px;
  text-align: center; font-weight: 600; white-space: nowrap;
  font-size: 11.5px; cursor: pointer; user-select: none;
  border-bottom: 1px solid var(--gray-300);
}
.race-table th:hover { background: var(--gray-200); color: var(--red); }
.race-table th.sorted-asc::after { content: ' ↑'; font-size: 11px; color: var(--red); font-weight: 700; }
.race-table th.sorted-desc::after { content: ' ↓'; font-size: 11px; color: var(--red); font-weight: 700; }

.race-table td { padding: 9px 8px; border-bottom: 1px solid var(--gray-200); text-align: center; vertical-align: middle; white-space: nowrap; }
.race-table td.name { text-align: left; white-space: nowrap; min-width: 180px; max-width: 260px; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.race-table tr:hover td { background: #FEF2F2; }
.race-table tr.dnf td { background: rgba(254, 243, 199, .4); }
.race-table tr.dns td, .race-table tr.dsq td { background: rgba(254, 226, 226, .35); }

/* Specific column widths */
.race-table th.col-rank, .race-table td.col-rank { width: 50px; font-weight: 700; }
.race-table th.col-bib,  .race-table td.col-bib  { width: 70px; font-family: 'JetBrains Mono', monospace; font-size: 11.5px; color: var(--gray-700); }
.race-table th.col-g,    .race-table td.col-g    { width: 36px; font-weight: 600; }
.race-table th.col-nat,  .race-table td.col-nat  { width: 50px; }
.race-table th.col-dist, .race-table td.col-dist { width: 80px; font-weight: 500; }
.race-table th.col-cat,  .race-table td.col-cat  { font-size: 10.5px; color: var(--gray-700); white-space: nowrap; }
.race-table th.col-time, .race-table td.col-time { width: 90px; font-family: 'JetBrains Mono', monospace; font-size: 11.5px; }
.race-table th.col-status, .race-table td.col-status { width: 70px; }
.race-table th.col-event,  .race-table td.col-event  { width: 90px; }

/* Status badges */
.badge { display: inline-block; padding: 3px 10px; border-radius: 10px; font-size: 10.5px; font-weight: 600; white-space: nowrap; }
.b-finish { background: #D1FAE5; color: #047857; }
.b-dnf    { background: #FEF3C7; color: #B45309; }
.b-dns    { background: #FEE2E2; color: #DC2626; }
.b-dsq    { background: #FEE2E2; color: #DC2626; }

/* Pagination */
.pager { display: flex; justify-content: center; align-items: center; gap: 6px; margin-top: 12px; flex-wrap: wrap; }
.pg-btn {
  padding: 5px 12px; border: 1px solid var(--gray-300); border-radius: 6px;
  background: #fff; font-size: 12px; cursor: pointer; font-family: inherit;
}
.pg-btn:hover { background: rgba(238, 68, 57, .08); }
.pg-btn.on { background: var(--red); color: #fff; border-color: var(--red); }
.pg-btn:disabled { opacity: .4; cursor: default; }
.pg-info { font-size: 11px; color: var(--gray-500); }

/* ===== FOOTER ===== */
.footer {
  border-top: 1px solid var(--gray-300);
  background: #fff; padding: 24px 32px;
  margin-top: 48px;
  text-align: center; font-size: 12px; color: var(--gray-500);
}
.footer .version { display: inline-block; padding: 2px 8px; background: var(--gray-200); border-radius: 10px; margin-left: 8px; }


/* ===== Race Up Logo (square icon mark + "Hub" wordmark) ===== */
.topnav-brand { gap: 8px; text-decoration: none; }
.topnav-brand .topnav-logo {
  height: 32px;
  width: 86px;          /* horizontal "race up" CI mark, aspect 960:357 ≈ 2.69 */
  display: block;
  flex: 0 0 auto;
  object-fit: contain;
}
@media (max-width: 600px) {
  .topnav-brand .topnav-logo { height: 28px; width: 75px; }
}

/* ============================================================
   Global mobile responsiveness (≤768px)
   Broad, low-risk rules every page inherits. Page-specific grids
   that still overflow get a 1-column @media in their own <style>.
   ============================================================ */
@media (max-width: 768px) {
  /* tighter gutters so content isn't crushed on a phone */
  .main { padding: 16px; }

  /* media never forces horizontal scroll */
  img, svg, canvas, video, iframe { max-width: 100%; height: auto; }

  /* wide tables scroll horizontally inside their own box instead of
     blowing the whole page sideways — user can still see every column */
  table {
    display: block;
    width: 100%;
    max-width: 100%;
    /* !important so it beats page rules like `table.crm{overflow:hidden}`
       (those exist for desktop border-radius clipping) — on a phone we
       MUST let wide tables scroll so every column stays reachable */
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  /* the mobile nav dropdown can get tall — let it scroll, and make
     submenus inline (not absolutely-positioned off-screen) */
  .topnav-links { max-height: calc(100vh - 56px); overflow-y: auto; }
  .topnav-submenu {
    position: static; box-shadow: none; border: none;
    padding-left: 14px; background: transparent;
  }
  .topnav-submenu::before { display: none; }
  .topnav-group.open .topnav-submenu { display: block; }
  /* level-2 groups: render inline-expanded on mobile (no flyout) */
  .topnav-submenu-2 {
    position: static; visibility: visible; opacity: 1; transform: none;
    pointer-events: auto; box-shadow: none; border: none;
    margin: 0 0 0 14px; padding: 2px 0; min-width: 0;
  }
  .topnav-submenu-2::before { display: none; }
  .topnav-sub-link.has-children .sg-caret { display: none; }
}
