/* ==========================================================================
   Kelikamerat — Tyylit
   OKLCH-paletti, glass-morphism (Kelin design-kielen mukaan)
   ========================================================================== */

@layer reset, base, layout, components, utilities;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html, body { margin: 0; padding: 0; height: 100%; }
  body {
    font-family: 'Manrope', system-ui, sans-serif;
    background: var(--bg);
    color: var(--fg);
    overflow: hidden;
  }
  button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
  input { font: inherit; }
}

@layer base {
  :root {
    /* OKLCH paletti */
    --bg:        oklch(18% 0.02 250);
    --bg-2:      oklch(22% 0.025 250);
    --surface:   oklch(26% 0.03 250 / 0.78);
    --surface-2: oklch(30% 0.035 250 / 0.85);
    --border:    oklch(40% 0.04 250 / 0.4);
    --fg:        oklch(96% 0.01 250);
    --fg-2:      oklch(78% 0.02 250);
    --fg-dim:    oklch(60% 0.02 250);
    --accent:    oklch(72% 0.18 220);
    --accent-2:  oklch(78% 0.16 200);
    --warn:      oklch(75% 0.16 60);
    --error:     oklch(68% 0.20 25);
    --ok:        oklch(72% 0.18 150);

    --r-sm: 6px;
    --r-md: 12px;
    --r-lg: 18px;

    --shadow-1: 0 4px 14px oklch(0% 0 0 / 0.25);
    --shadow-2: 0 12px 40px oklch(0% 0 0 / 0.45);

    --topbar-h: 64px;
    --sidebar-w: 360px;
    --drawer-w: 480px;

    --t-fast: 120ms ease;
    --t-mid:  240ms cubic-bezier(.2,.7,.2,1);
  }

  h1, h2, h3, h4 {
    font-family: 'Rajdhani', system-ui, sans-serif;
    font-weight: 600;
    letter-spacing: 0.01em;
    margin: 0;
  }
  .mono { font-family: 'JetBrains Mono', monospace; }
}

@layer layout {
  #app {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    grid-template-rows: var(--topbar-h) 1fr;
    grid-template-areas:
      "topbar topbar"
      "side   map";
    height: 100dvh;
  }

  .topbar  { grid-area: topbar; }
  .sidebar { grid-area: side; }
  .map     { grid-area: map; }

  @media (max-width: 768px) {
    #app {
      grid-template-columns: 1fr;
      grid-template-rows: var(--topbar-h) 1fr;
      grid-template-areas:
        "topbar"
        "map";
    }
    .sidebar.open {
      grid-area: map;
      z-index: 460;
      background: var(--bg-2);
    }
    .sidebar:not(.open) { display: none; }
  }

  .boot {
    display: grid; place-items: center;
    height: 100dvh;
    color: var(--fg-2);
    font-family: 'Rajdhani', system-ui, sans-serif;
    letter-spacing: 0.05em;
    text-transform: uppercase;
  }
}

@layer components {
  /* Topbar + hakukenttä */
  .topbar {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 0 16px;
    background: var(--surface);
    backdrop-filter: blur(14px) saturate(140%);
    -webkit-backdrop-filter: blur(14px) saturate(140%);
    border-bottom: 1px solid var(--border);
    z-index: 500;
  }
  .topbar .brand {
    font-family: 'Rajdhani', system-ui, sans-serif;
    font-weight: 700;
    font-size: 20px;
    letter-spacing: 0.04em;
    color: var(--fg);
    white-space: nowrap;
  }
  .topbar .brand .accent { color: var(--accent); }

  .search {
    position: relative;
    flex: 1;
    max-width: 640px;
  }
  .search input {
    width: 100%;
    padding: 10px 36px 10px 38px;
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    color: var(--fg);
    outline: none;
    transition: border-color var(--t-fast), box-shadow var(--t-fast);
  }
  .search input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px oklch(72% 0.18 220 / 0.18);
  }
  .search .icon {
    position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
    color: var(--fg-dim);
    pointer-events: none;
  }
  .search .clear {
    position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
    color: var(--fg-dim);
    padding: 4px 6px;
    border-radius: 4px;
  }
  .search .clear:hover { color: var(--fg); background: var(--surface-2); }

  .count {
    font-family: 'JetBrains Mono', monospace;
    font-size: 13px;
    color: var(--fg-2);
    white-space: nowrap;
  }
  .list-toggle {
    display: none;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    color: var(--fg);
    white-space: nowrap;
  }
  .list-toggle .lt-count {
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    color: var(--fg-2);
    padding: 1px 6px;
    background: var(--bg-2);
    border-radius: 10px;
  }
  @media (max-width: 768px) {
    .list-toggle { display: inline-flex; padding: 8px 10px; }
    .list-toggle .lt-label { display: none; }
    .topbar .brand { display: none; }
    .topbar { gap: 8px; padding: 0 10px; }
    .topbar .count { display: none; }
  }

  /* Sidebar — tuloslista */
  .sidebar {
    background: var(--bg-2);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    overflow-x: hidden;
  }
  .results { padding: 8px; display: flex; flex-direction: column; gap: 4px; }
  .result {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 10px 12px;
    border-radius: var(--r-sm);
    border: 1px solid transparent;
    text-align: left;
    width: 100%;
    transition: background var(--t-fast), border-color var(--t-fast);
  }
  .result:hover { background: var(--surface); border-color: var(--border); }
  .result.selected { background: var(--surface-2); border-color: var(--accent); }
  .result .name { font-weight: 600; color: var(--fg); }
  .result .meta { font-size: 12px; color: var(--fg-dim); font-family: 'JetBrains Mono', monospace; }

  .empty, .skel {
    padding: 24px 16px;
    color: var(--fg-dim);
    text-align: center;
    font-size: 14px;
  }
  .skel-row {
    height: 44px;
    margin: 6px 8px;
    border-radius: var(--r-sm);
    background: linear-gradient(90deg,
      var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%);
    background-size: 200% 100%;
    animation: shimmer 1.4s infinite linear;
  }
  @keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

  /* Kartta */
  .map { position: relative; }
  .map-root { width: 100%; height: 100%; background: var(--bg-2); }

  .map-spinner {
    position: absolute; inset: 0;
    display: grid; place-items: center;
    pointer-events: none;
  }
  .spinner {
    width: 36px; height: 36px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.9s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }

  /* Route toggle nappi topbarissa */
  .route-toggle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    color: var(--fg);
    font-size: 13px;
    transition: all var(--t-fast);
  }
  .route-toggle:hover { border-color: var(--accent); }
  .route-toggle.active {
    background: var(--accent);
    border-color: var(--accent);
    color: oklch(15% 0.02 250);
  }
  @media (max-width: 768px) {
    .route-toggle span { display: none; }
  }

  /* Reitti-paneeli */
  .route-panel {
    position: absolute;
    top: var(--topbar-h);
    left: var(--sidebar-w);
    right: 0;
    background: var(--surface);
    backdrop-filter: blur(14px) saturate(140%);
    -webkit-backdrop-filter: blur(14px) saturate(140%);
    border-bottom: 1px solid var(--border);
    padding: 14px 18px;
    z-index: 450;
    display: flex;
    flex-direction: column;
    gap: 10px;
    box-shadow: var(--shadow-1);
  }
  @media (max-width: 768px) {
    .route-panel { left: 0; }
  }
  .route-row {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    align-items: flex-end;
  }
  .route-input { flex: 1; min-width: 220px; }
  .route-input label {
    display: block;
    font-size: 12px;
    color: var(--fg-dim);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 4px;
  }
  .autocomplete { position: relative; }
  .autocomplete input {
    width: 100%;
    padding: 9px 32px 9px 12px;
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    color: var(--fg);
    outline: none;
  }
  .autocomplete input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px oklch(72% 0.18 220 / 0.18);
  }
  .ac-busy {
    position: absolute; right: 30px; top: 50%; transform: translateY(-50%);
    color: var(--fg-dim); font-size: 16px;
  }
  .ac-clear {
    position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
    color: var(--fg-dim);
    padding: 4px 6px; border-radius: 4px;
  }
  .ac-clear:hover { color: var(--fg); background: var(--surface-2); }
  .ac-list {
    position: absolute; top: calc(100% + 4px); left: 0; right: 0;
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    list-style: none; margin: 0; padding: 4px;
    z-index: 600;
    max-height: 240px; overflow-y: auto;
    box-shadow: var(--shadow-1);
  }
  .ac-list li button {
    display: block; width: 100%; text-align: left;
    padding: 8px 10px; border-radius: var(--r-sm);
    color: var(--fg); font-size: 13px;
  }
  .ac-list li button:hover { background: var(--surface-2); }

  .buffer-row { gap: 16px; }
  .buffer-label {
    display: flex; align-items: center; gap: 8px;
    font-size: 13px; color: var(--fg-2);
  }
  .buffer-label input[type=range] {
    accent-color: var(--accent);
    width: 140px;
  }

  .route-btn {
    padding: 9px 14px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    color: var(--fg);
    font-weight: 500;
  }
  .route-btn:hover:not(:disabled) { border-color: var(--accent); }
  .route-btn:disabled { opacity: 0.5; cursor: not-allowed; }
  .route-btn.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: oklch(15% 0.02 250);
    font-weight: 600;
  }
  .route-btn.primary:hover:not(:disabled) {
    background: oklch(78% 0.18 220);
  }

  .route-error {
    color: var(--error);
    font-size: 13px;
  }
  .route-summary {
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    color: var(--fg-2);
  }

  /* Custom kamera-ikoni */
  .cam-icon {
    width: 28px; height: 28px;
    background: oklch(26% 0.03 250 / 0.92);
    border: 2px solid var(--accent);
    border-radius: 50%;
    display: grid; place-items: center;
    color: var(--accent-2);
    box-shadow: 0 2px 6px oklch(0% 0 0 / 0.5);
    transition: transform var(--t-fast);
  }
  .cam-icon:hover { transform: scale(1.15); }
  .cam-icon.dim { opacity: 0.3; filter: grayscale(0.8); }

  /* Drawer */
  /* Overlay peittää vain kartta-alueen, EI sidebaria — listaa pitää voida selailla
     samalla kun drawer on auki, jotta reitin kameroita voi klikata nopeasti läpi. */
  .drawer-overlay {
    position: fixed;
    top: var(--topbar-h);
    left: var(--sidebar-w);
    right: 0;
    bottom: 0;
    background: oklch(0% 0 0 / 0.25);
    z-index: 800;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--t-mid);
  }
  .drawer-overlay.open { opacity: 1; pointer-events: auto; }
  @media (max-width: 768px) {
    /* Mobiilissa lista ei muutenkaan ole näkyvissä → täysruutu-overlay. */
    .drawer-overlay { top: 0; left: 0; }
  }

  .drawer {
    position: fixed;
    top: 0; right: 0; bottom: 0;
    width: var(--drawer-w);
    max-width: 100vw;
    background: var(--bg-2);
    border-left: 1px solid var(--border);
    box-shadow: var(--shadow-2);
    z-index: 900;
    transform: translateX(100%);
    transition: transform var(--t-mid);
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
  .drawer.open { transform: translateX(0); }

  @media (max-width: 768px) {
    .drawer {
      top: auto;
      width: 100vw;
      height: 85dvh;
      border-left: none;
      border-top: 1px solid var(--border);
      border-radius: var(--r-lg) var(--r-lg) 0 0;
      transform: translateY(100%);
    }
    .drawer.open { transform: translateY(0); }
  }

  .drawer header {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 72px;
  }
  .drawer header h2 {
    flex: 1;
    font-size: 17px;
    font-weight: 600;
    color: var(--fg);
    line-height: 1.25;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .drawer header .road {
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    color: var(--fg-dim);
    margin-top: 2px;
  }
  .drawer .close {
    width: 32px; height: 32px;
    border-radius: var(--r-sm);
    color: var(--fg-2);
    display: grid; place-items: center;
  }
  .drawer .close:hover { background: var(--surface-2); color: var(--fg); }

  .drawer-nav {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    padding: 2px;
  }
  .drawer-nav .nav-btn {
    width: 44px; height: 44px;
    display: grid; place-items: center;
    color: var(--fg);
    border-radius: var(--r-sm);
    font-size: 26px;
    line-height: 1;
    transition: background var(--t-fast), color var(--t-fast);
  }
  .drawer-nav .nav-btn:hover:not(:disabled) {
    background: var(--accent);
    color: oklch(15% 0.02 250);
  }
  .drawer-nav .nav-btn:disabled { opacity: 0.3; cursor: not-allowed; }
  .drawer-nav .nav-idx {
    min-width: 52px;
    text-align: center;
    font-size: 13px;
    color: var(--fg-2);
    user-select: none;
  }

  .drawer .body {
    overflow-y: auto;
    padding: 16px 20px 24px;
    flex: 1;
  }

  .presets {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 12px;
  }
  .preset-tab {
    padding: 6px 10px;
    font-size: 13px;
    font-family: 'JetBrains Mono', monospace;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    color: var(--fg-2);
    transition: all var(--t-fast);
  }
  .preset-tab:hover { color: var(--fg); border-color: var(--accent); }
  .preset-tab.active {
    background: var(--accent);
    border-color: var(--accent);
    color: oklch(15% 0.02 250);
    font-weight: 600;
  }

  .cam-image {
    width: 100%;
    aspect-ratio: 4 / 3;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    overflow: hidden;
    position: relative;
  }
  .cam-image img {
    width: 100%; height: 100%;
    object-fit: cover;
    display: block;
  }
  .cam-image .placeholder {
    position: absolute; inset: 0;
    display: grid; place-items: center;
    color: var(--fg-dim);
    font-size: 14px;
  }
  .updated {
    margin-top: 8px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    color: var(--fg-dim);
  }

  .preset-meta {
    margin-top: 6px;
    color: var(--fg-2);
    font-size: 13px;
  }

  .weather {
    margin-top: 18px;
    padding-top: 14px;
    border-top: 1px solid var(--border);
  }
  .weather h3 {
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--fg-dim);
    margin-bottom: 10px;
  }
  .weather dl {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: 6px 16px;
    margin: 0;
  }
  .weather dt { color: var(--fg-dim); font-size: 13px; }
  .weather dd {
    margin: 0;
    font-family: 'JetBrains Mono', monospace;
    color: var(--fg);
    font-size: 14px;
  }
  .weather .age {
    margin-top: 10px;
    font-size: 11px;
    color: var(--fg-dim);
  }

  /* Virhebanneri */
  .banner {
    position: absolute;
    top: calc(var(--topbar-h) + 12px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--surface);
    backdrop-filter: blur(10px);
    border: 1px solid var(--error);
    color: var(--fg);
    padding: 10px 14px;
    border-radius: var(--r-md);
    z-index: 700;
    display: flex; align-items: center; gap: 12px;
    box-shadow: var(--shadow-1);
  }
  .banner button {
    padding: 4px 10px;
    background: var(--error);
    color: oklch(15% 0.02 250);
    border-radius: var(--r-sm);
    font-weight: 600;
  }
}

@layer utilities {
  /* Leaflet override — match dark theme */
  .leaflet-container { background: var(--bg-2); }
  .leaflet-control-attribution {
    background: var(--surface) !important;
    color: var(--fg-dim) !important;
    backdrop-filter: blur(8px);
  }
  .leaflet-control-attribution a { color: var(--accent-2) !important; }
}
