:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1c1d22;
  --muted: #6b7280;
  --border: #e3e5e9;
  --accent: #e2231a;      /* Homepro red */
  --in: #0f7b41;
  --in-bg: #e7f5ec;
  --zero: #9aa0aa;
  --zero-bg: #f1f2f4;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 6px 20px rgba(0,0,0,.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14151a;
    --card: #1e2027;
    --text: #e8eaed;
    --muted: #9aa0aa;
    --border: #2c2f38;
    --accent: #ff5a4d;
    --in: #4ade80;
    --in-bg: #133427;
    --zero: #6b7280;
    --zero-bg: #23252c;
    --shadow: 0 1px 3px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai",
               "Sarabun", Tahoma, Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.5;
}

.wrap {
  max-width: 640px;
  margin: 0 auto;
  padding: 32px 16px 16px;
}

h1 {
  font-size: 1.5rem;
  margin: 0 0 4px;
}

.sub {
  margin: 0 0 20px;
  color: var(--muted);
  font-size: .95rem;
}

.search-bar {
  display: flex;
  gap: 8px;
}

.search-bar input {
  flex: 1;
  min-width: 0;
  padding: 12px 14px;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
}

.search-bar input:focus,
.filter:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

.search-bar button {
  position: relative;
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 10px;
  cursor: pointer;
  white-space: nowrap;
}

.search-bar button:disabled {
  opacity: .6;
  cursor: default;
}

/* Spinner shown on the search button while a lookup is in flight. It's absolutely
   positioned and the label is hidden with visibility, so the button never resizes. */
.btn-spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 16px;
  height: 16px;
  margin: -8px 0 0 -8px;
  border: 2px solid rgba(255, 255, 255, .45);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
  display: none;
}
.search-bar button.loading .btn-label { visibility: hidden; }
.search-bar button.loading .btn-spinner { display: block; }
.search-bar button.loading { opacity: 1; } /* keep the spinner crisp even though disabled */

.status {
  margin-top: 16px;
  padding: 12px 14px;
  border-radius: 10px;
  font-size: .95rem;
  background: var(--card);
  border: 1px solid var(--border);
}

.status.loading { color: var(--muted); }
.status.error {
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 40%, var(--border));
  background: color-mix(in srgb, var(--accent) 8%, var(--card));
}

#resultArea { margin-top: 20px; }

.filters {
  position: sticky;
  top: 8px;
  z-index: 2;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.zone-select {
  width: 100%;
  padding: 12px 14px;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  box-shadow: var(--shadow);
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='%236b7280' d='M1 1l5 5 5-5'/></svg>");
  background-repeat: no-repeat;
  background-position: right 14px center;
  padding-right: 36px;
}

.filter-wrap {
  position: relative;
}

.filter {
  width: 100%;
  padding: 12px 40px 12px 14px; /* right room for the clear button */
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  box-shadow: var(--shadow);
}

.filter-clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  padding: 0;
  display: none;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
}
.filter-clear:hover { color: var(--text); background: var(--border); }
.filter-wrap.has-text .filter-clear { display: flex; }

.list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 14px 2px 8px;
}

.count {
  font-size: .85rem;
  color: var(--muted);
}

.sort-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: none;
}

.sort-sep {
  color: var(--muted);
  opacity: .5;
}

.sort-btn {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 4px 2px;
  background: none;
  border: none;
  cursor: pointer;
  font: inherit;
  font-size: .85rem;
  color: var(--muted);
}
.sort-btn:hover { color: var(--text); }
.sort-btn.active { color: var(--text); font-weight: 600; }
.sort-btn .arrow { font-size: .9em; }

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Mobile: keep the results area tall so narrowing the filter can't shrink the
   page enough to scroll the sticky search field off screen while you're typing. */
@media (max-width: 640px) {
  .list { min-height: 75vh; }
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.row.out { opacity: .72; }

.info { min-width: 0; }

.row .name {
  font-weight: 600;
  font-size: 1rem;
}

.row .hour {
  color: var(--muted);
  font-size: .82rem;
  margin-top: 2px;
}

.stock {
  flex: none;
  display: flex;
  align-items: baseline;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
}

.stock.in { color: var(--in); background: var(--in-bg); }
.stock.zero { color: var(--zero); background: var(--zero-bg); }

.stock .num { font-size: 1.15rem; font-weight: 700; }
.stock .unit { font-size: .78rem; }

.empty {
  list-style: none;
  padding: 24px;
  text-align: center;
  color: var(--muted);
  background: var(--card);
  border: 1px dashed var(--border);
  border-radius: 10px;
}

.map-title {
  font-size: 1.05rem;
  margin: 28px 0 8px;
}

.map {
  height: 440px;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: var(--shadow);
  margin-bottom: 20px;
}

/* Leaflet popup/tooltip text stays readable in dark mode (map tiles are light). */
.map .leaflet-popup-content,
.map .leaflet-tooltip {
  color: #1c1d22;
  font-family: inherit;
  line-height: 1.45;
}
.map .pop-hour { color: #555; font-size: .85em; }
.map .pop-stock { color: #0f7b41; font-weight: 700; }
.map .pop-gmaps {
  display: inline-block;
  margin-top: 6px;
  color: #1a73e8;
  font-weight: 600;
  text-decoration: none;
}
.map .pop-gmaps:hover { text-decoration: underline; }

.geo-status {
  text-align: center;
  font-size: .85rem;
  color: var(--muted);
  padding: 10px 16px 40px;
}
.geo-status.ok { color: var(--in); }
.geo-status.warn { color: var(--accent); }

.spinner {
  display: inline-block;
  width: 13px;
  height: 13px;
  vertical-align: -2px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.hidden { display: none; }
