/* HS8AC Global Grid Locator - design system
   Modern Radio Map / Network Operations aesthetic: deep navy + electric blue
   + amber, off-white text, status colors kept separate from brand colors so
   "approximate" (yellow) never reads as "brand amber". No template look,
   minimal motion, readable outdoors and at night. */

:root {
  /* dark theme (default) */
  --bg: #0a1220;
  --surface: #101a2e;
  --surface-2: #17233d;
  --surface-3: #1e2c4a;
  --border: #2a3a5c;
  --text: #f4f1ea;
  --text-muted: #9fb0cc;
  --text-faint: #6b7c9e;
  --accent: #3aa0ff;
  --accent-strong: #6ab8ff;
  --accent-2: #f2994a;
  --gps: #34d399;
  --approx: #eab308;
  --error: #ef4444;
  --focus-ring: #6ab8ff;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-elevated: 0 8px 24px rgba(0, 0, 0, 0.35);
  --sidebar-width: 380px;
  --header-height: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  --transition-fast: 120ms ease;
  --transition-normal: 200ms ease;
}

:root[data-theme="light"] {
  --bg: #eef2f8;
  --surface: #ffffff;
  --surface-2: #eaf0fa;
  --surface-3: #dde7f5;
  --border: #cdd8ea;
  --text: #0f1b2d;
  --text-muted: #47597a;
  --text-faint: #7285a6;
  --accent: #1c7ed6;
  --accent-strong: #1565bd;
  --accent-2: #c5680f;
  --gps: #15803d;
  --approx: #a16207;
  --error: #dc2626;
  --focus-ring: #1c7ed6;
  --shadow-elevated: 0 8px 24px rgba(15, 27, 45, 0.15);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #eef2f8;
    --surface: #ffffff;
    --surface-2: #eaf0fa;
    --surface-3: #dde7f5;
    --border: #cdd8ea;
    --text: #0f1b2d;
    --text-muted: #47597a;
    --text-faint: #7285a6;
    --accent: #1c7ed6;
    --accent-strong: #1565bd;
    --accent-2: #c5680f;
    --gps: #15803d;
    --approx: #a16207;
    --error: #dc2626;
    --focus-ring: #1c7ed6;
    --shadow-elevated: 0 8px 24px rgba(15, 27, 45, 0.15);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family:
    "Noto Sans Thai",
    "Noto Sans",
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    Roboto,
    sans-serif;
  font-size: 15px;
  line-height: 1.5;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

img {
  max-width: 100%;
}

a {
  color: var(--accent-strong);
}

button {
  font-family: inherit;
}

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

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

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

.skip-link {
  position: absolute;
  top: -100px;
  left: 8px;
  z-index: 1000;
  background: var(--accent);
  color: #051020;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  transition: top var(--transition-fast);
}
.skip-link:focus {
  top: 8px;
}

/* ---------- App shell ---------- */

.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.app-header {
  height: var(--header-height);
  padding-top: var(--safe-top);
  display: flex;
  align-items: center;
  gap: 12px;
  padding-left: max(16px, var(--safe-left));
  padding-right: max(16px, var(--safe-right));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-logo {
  width: 30px;
  height: 30px;
  flex-shrink: 0;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
  min-width: 0;
}

.brand-name {
  font-weight: 800;
  font-size: 13px;
  letter-spacing: 0.03em;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-tagline {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-spacer {
  flex: 1;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  transition: background var(--transition-fast), border-color var(--transition-fast);
}
.icon-btn:hover {
  background: var(--surface-3);
  border-color: var(--accent);
}
.icon-btn svg {
  width: 20px;
  height: 20px;
}

.lang-btn {
  width: auto;
  padding: 0 12px;
  font-weight: 700;
  font-size: 13px;
}

/* ---------- Main layout ---------- */

.app-main {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

/* .control-panel is ONE element, restyled as a desktop sidebar or a mobile
   bottom sheet by the breakpoint below -- no duplicated markup/IDs. Its
   `data-open` attribute means "sidebar visible" on desktop and "sheet
   expanded" on mobile: same toggle, different visual result per breakpoint. */
.control-panel {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
  transition: margin-left var(--transition-normal);
}

.control-panel[data-open="false"] {
  margin-left: calc(var(--sidebar-width) * -1);
}

.sidebar-toggle-desktop {
  position: absolute;
  top: 12px;
  left: calc(var(--sidebar-width) + 12px);
  z-index: 30;
  transition: left var(--transition-normal);
}
.control-panel[data-open="false"] ~ .sidebar-toggle-desktop {
  left: 12px;
}

.bottom-sheet-handle {
  display: none;
}

.map-area {
  flex: 1;
  position: relative;
  min-width: 0;
}

#map {
  position: absolute;
  inset: 0;
  background: var(--surface-2);
}

.leaflet-container {
  background: var(--surface-2);
  font-family: inherit;
}

/* ---------- Panels / cards ---------- */

.panel {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
}

.panel-title {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin: 0 0 10px 0;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}
.field:last-child {
  margin-bottom: 0;
}

.field label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

.text-input,
.search-input {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
}
.text-input:focus,
.search-input:focus {
  border-color: var(--accent);
}

.input-error {
  font-size: 12px;
  color: var(--error);
  display: none;
}
.input-error[data-visible="true"] {
  display: block;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 16px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface-3);
  color: var(--text);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: background var(--transition-fast), transform var(--transition-fast);
}
.btn:hover {
  background: var(--surface-2);
}
.btn:active {
  transform: scale(0.98);
}
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #051020;
}
.btn-primary:hover {
  background: var(--accent-strong);
}
.btn-block {
  width: 100%;
}
.btn-sm {
  min-height: 36px;
  padding: 0 12px;
  font-size: 13px;
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

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

/* ---------- Status badges (icon + color + text; never color alone) ---------- */

.status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  border: 1px solid transparent;
}
.status svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}
.status-gps {
  color: var(--gps);
  border-color: color-mix(in srgb, var(--gps) 40%, transparent);
  background: color-mix(in srgb, var(--gps) 12%, transparent);
}
.status-approx {
  color: var(--approx);
  border-color: color-mix(in srgb, var(--approx) 40%, transparent);
  background: color-mix(in srgb, var(--approx) 12%, transparent);
}
.status-error {
  color: var(--error);
  border-color: color-mix(in srgb, var(--error) 40%, transparent);
  background: color-mix(in srgb, var(--error) 12%, transparent);
}
.status-info {
  color: var(--accent-strong);
  border-color: color-mix(in srgb, var(--accent) 40%, transparent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

/* ---------- Result card ---------- */

.result-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
}

.result-title {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  margin: 0 0 6px 0;
}

.result-grid-primary {
  font-size: 34px;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: var(--accent-strong);
  font-variant-numeric: tabular-nums;
  word-break: break-all;
}

.result-grid-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin: 12px 0;
}

.result-grid-chip {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
}
.result-grid-chip .chip-label {
  font-size: 10px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.result-grid-chip .chip-value {
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.result-meta {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-top: 12px;
  font-size: 13px;
}

.result-meta-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}
.result-meta-row:last-child {
  border-bottom: none;
}
.result-meta-row .meta-label {
  color: var(--text-muted);
}
.result-meta-row .meta-value {
  text-align: right;
  font-weight: 600;
}

.boundary-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin: 12px 0;
  padding: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.boundary-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  min-height: 24px;
}
.boundary-toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent-2);
}

.boundary-precision {
  display: inline-flex;
  gap: 4px;
  margin-left: auto;
}

.precision-btn {
  min-width: 36px;
  min-height: 32px;
  padding: 0 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-muted);
  font-weight: 700;
  font-size: 12px;
  cursor: pointer;
}
.precision-btn[aria-pressed="true"] {
  background: var(--accent-2);
  border-color: var(--accent-2);
  color: #2a1600;
}

.boundary-note {
  flex-basis: 100%;
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
}

.result-empty {
  color: var(--text-muted);
  font-size: 14px;
  text-align: center;
  padding: 24px 8px;
}

/* ---------- Disambiguation list (postal code / name matches) ---------- */

.result-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 320px;
  overflow-y: auto;
}

.result-list-item {
  width: 100%;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  cursor: pointer;
  color: var(--text);
}
.result-list-item:hover {
  border-color: var(--accent);
}
.result-list-item .item-primary {
  font-weight: 700;
  font-size: 14px;
}
.result-list-item .item-secondary {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* ---------- Privacy notice ---------- */

.privacy-notice {
  font-size: 12px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  padding-top: 12px;
  margin-top: 4px;
}

/* ---------- Map floating controls ---------- */

.map-fab {
  position: absolute;
  right: max(16px, var(--safe-right));
  bottom: max(16px, var(--safe-bottom));
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: #051020;
  border: none;
  box-shadow: var(--shadow-elevated);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 15;
}
.map-fab svg {
  width: 26px;
  height: 26px;
}
.map-fab[data-active="true"] {
  background: var(--gps);
}

.map-toggle-overlay {
  position: absolute;
  left: max(12px, var(--safe-left));
  top: 12px;
  z-index: 15;
}

.grid-label {
  background: color-mix(in srgb, var(--surface) 85%, transparent);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent-strong);
  white-space: nowrap;
  pointer-events: none;
}


/* ---------- Tabs (Locator / Distance) ---------- */

.view-tabs {
  display: flex;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 4px;
}
.view-tab {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-weight: 700;
  font-size: 13px;
  padding: 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.view-tab[aria-selected="true"] {
  background: var(--accent);
  color: #051020;
}

.view-panel {
  display: none;
  flex-direction: column;
  gap: 16px;
}
.view-panel[data-active="true"] {
  display: flex;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, calc(var(--safe-bottom) + 12px));
  transform: translateX(-50%) translateY(20px);
  background: var(--surface-3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow-elevated);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-normal), transform var(--transition-normal);
  z-index: 100;
  max-width: 90vw;
}
.toast[data-visible="true"] {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ================= Responsive ================= */

@media (max-width: 767px) {
  .brand-tagline {
    display: none;
  }

  .sidebar-toggle-desktop {
    display: none;
  }

  .app-main {
    flex-direction: column;
  }

  .map-area {
    flex: 1;
  }

  /* .control-panel becomes the bottom sheet on mobile */
  .control-panel {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    border-right: none;
    border: 1px solid var(--border);
    border-top-left-radius: var(--radius-lg);
    border-top-right-radius: var(--radius-lg);
    box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.3);
    z-index: 18;
    max-height: 72dvh;
    margin-left: 0;
    padding: 0;
    transform: translateY(calc(100% - 108px - var(--safe-bottom)));
    transition: transform var(--transition-normal);
  }
  .control-panel[data-open="true"] {
    transform: translateY(0);
  }
  .control-panel[data-open="false"] {
    margin-left: 0; /* override the desktop off-canvas rule */
  }

  .bottom-sheet-handle {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px 0 6px 0;
    cursor: pointer;
    flex-shrink: 0;
    min-height: 44px;
  }
  .bottom-sheet-handle .grabber {
    width: 40px;
    height: 4px;
    border-radius: 2px;
    background: var(--border);
  }

  .bottom-sheet-summary {
    padding: 0 16px 12px 16px;
    flex-shrink: 0;
  }

  .bottom-sheet-body {
    overflow-y: auto;
    padding: 0 16px calc(16px + var(--safe-bottom)) 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .map-fab {
    bottom: max(124px, calc(108px + var(--safe-bottom) + 16px));
  }

  .result-grid-primary {
    font-size: 28px;
  }
}

@media (min-width: 768px) {
  .icon-btn.mobile-only {
    display: none;
  }

  .bottom-sheet-summary {
    display: none;
  }
}

/* No page ever scrolls horizontally regardless of content. */
html,
body {
  max-width: 100%;
}
