:root {
  --ink: #17211b;
  --muted: #657166;
  --line: #d7dfd4;
  --paper: #f7f5ec;
  --panel: #fffdf6;
  --accent: #d55332;
  --accent-dark: #9f341f;
  --green: #0f766e;
  --shadow: 0 18px 45px rgba(31, 41, 34, 0.16);
}

* { box-sizing: border-box; }

/* Ensure the HTML `hidden` attribute always wins over display:flex/grid rules */
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--ink);
  background:
    radial-gradient(circle at 14% 18%, rgba(213, 83, 50, 0.16), transparent 26rem),
    linear-gradient(135deg, #f7f5ec 0%, #e8efe6 100%);
  font-family: "Noto Serif TC", "Microsoft JhengHei", serif;
}

button, input {
  font: inherit;
}

button, a {
  border-radius: 7px;
}

button {
  border: 0;
  background: var(--accent);
  color: white;
  cursor: pointer;
  font-weight: 700;
}

button:hover, a:hover {
  filter: brightness(0.96);
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 16px;
  background: rgba(255, 253, 246, 0.95);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.summary-text {
  color: var(--muted);
  font-size: 0.88rem;
}

.search-bar {
  display: flex;
  gap: 8px;
  padding: 8px 16px;
  background: rgba(255, 253, 246, 0.92);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.search-bar input {
  flex: 1 1 0;
  min-width: 0;
}

.search-bar button {
  flex-shrink: 0;
  padding: 10px 14px;
  font-size: 0.88rem;
}

.sidebar {
  display: flex;
  flex-direction: column;
  flex: 1 1 0;
  min-height: 0;
  background: rgba(255, 253, 246, 0.92);
  border-top: 1px solid var(--line);
}

.detail-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent-dark);
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1, h2 {
  margin: 0;
  line-height: 1.1;
}

h1 { font-size: clamp(1.55rem, 3vw, 2.2rem); }
h2 { font-size: 1.35rem; }

.ghost-button {
  padding: 8px 11px;
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
}

.logout-button {
  padding: 6px 9px;
  font-size: 0.82rem;
  font-weight: 800;
}

.filters {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

input {
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 12px 13px;
  background: white;
  color: var(--ink);
}

.segments {
  display: flex;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}

.segments button {
  border-radius: 0;
  padding: 8px 12px;
  background: white;
  color: var(--muted);
  font-size: 0.86rem;
}

.segments button.active {
  background: var(--green);
  color: white;
}

/* Grade switcher uses accent colour to distinguish from status filter */
.grade-segments button.active {
  background: var(--accent);
  color: white;
}

/* On narrow screens let the two filter groups wrap to a second row */
.filters {
  flex-wrap: wrap;
}

.detail-actions button, .login-form button {
  padding: 10px 13px;
}

.spot-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 8px;
  overflow: auto;
  padding: 10px 12px;
  align-content: start;
}

.spot-card {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  text-align: left;
  background: white;
  color: var(--ink);
  border: 1px solid var(--line);
}

.spot-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(213, 83, 50, 0.14);
}

.spot-status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--line);
  flex-shrink: 0;
}

.spot-status-dot.done {
  background: var(--green);
}

.spot-title {
  font-weight: 700;
  font-size: 0.9rem;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.map-panel {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
}

#map {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.leaflet-container {
  overflow: hidden;
  touch-action: pan-x pan-y;
}

.leaflet-pane,
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow,
.leaflet-tile-container,
.leaflet-pane > svg,
.leaflet-pane > canvas,
.leaflet-zoom-box,
.leaflet-image-layer,
.leaflet-layer {
  position: absolute;
  left: 0;
  top: 0;
}

.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow {
  user-select: none;
}

.leaflet-tile {
  border: 0;
  visibility: hidden;
}

.leaflet-tile-loaded {
  visibility: inherit;
}

.leaflet-pane { z-index: 400; }
.leaflet-tile-pane { z-index: 200; }
.leaflet-overlay-pane { z-index: 400; }
.leaflet-marker-pane { z-index: 600; }
.leaflet-tooltip-pane { z-index: 650; }
.leaflet-popup-pane { z-index: 700; }

.leaflet-control-container .leaflet-top,
.leaflet-control-container .leaflet-bottom {
  position: absolute;
  z-index: 800;
  pointer-events: none;
}

.leaflet-control-container .leaflet-top { top: 0; }
.leaflet-control-container .leaflet-right { right: 0; }
.leaflet-control-container .leaflet-bottom { bottom: 0; }
.leaflet-control-container .leaflet-left { left: 0; }

.leaflet-control {
  position: relative;
  z-index: 800;
  pointer-events: auto;
}

.leaflet-control-attribution {
  max-width: 46vw;
  padding: 1px 4px;
  background: rgba(255, 255, 255, 0.72);
  color: var(--muted);
  font-size: 0.62rem;
  line-height: 1.2;
}

.leaflet-control-attribution a {
  color: inherit;
}

.spot-map-marker {
  display: block;
  width: 24px;
  height: 24px;
  border: 3px solid white;
  border-radius: 50% 50% 50% 6px;
  background: var(--accent);
  box-shadow: 0 8px 18px rgba(23, 33, 27, 0.32);
  transform: rotate(-45deg);
  transition: transform 0.15s, box-shadow 0.15s;
}

.spot-map-marker::after {
  content: "";
  position: absolute;
  inset: 5px;
  border-radius: 50%;
  background: white;
}

.spot-map-marker.completed {
  background: var(--green);
}

.spot-map-marker.selected {
  transform: rotate(-45deg) scale(1.35);
  box-shadow: 0 0 0 5px rgba(213, 83, 50, 0.28), 0 8px 20px rgba(23, 33, 27, 0.38);
}

.spot-map-marker.completed.selected {
  box-shadow: 0 0 0 5px rgba(15, 118, 110, 0.28), 0 8px 20px rgba(23, 33, 27, 0.38);
}

.detail-panel {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  width: min(540px, calc(100vw - 24px));
  max-height: min(55dvh, 440px);
  display: flex;
  flex-direction: column;
  border-radius: 14px;
  background: rgba(255, 253, 246, 0.97);
  box-shadow: 0 8px 40px rgba(23, 33, 27, 0.22), 0 2px 8px rgba(23, 33, 27, 0.08);
  border: 1px solid var(--line);
  z-index: 500;
  overflow: hidden;
  animation: slideUp 0.22s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes slideUp {
  from { transform: translateX(-50%) translateY(18px); opacity: 0; }
  to   { transform: translateX(-50%) translateY(0);   opacity: 1; }
}

.detail-header {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 14px 16px 12px;
  flex-shrink: 0;
  border-bottom: 1px solid var(--line);
}

.detail-header .spot-status-dot {
  width: 11px;
  height: 11px;
  margin-top: 5px;
  flex-shrink: 0;
}

.detail-name {
  flex: 1;
  font-size: 1.05rem;
  font-weight: 900;
  line-height: 1.25;
  margin: 0;
  color: var(--ink);
}

.detail-close {
  padding: 4px 8px;
  font-size: 1rem;
  line-height: 1;
  flex-shrink: 0;
  margin-top: -2px;
}

.detail-actions-bar {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  flex-shrink: 0;
  flex-wrap: wrap;
  border-bottom: 1px solid var(--line);
  background: rgba(241, 247, 237, 0.6);
}

.detail-complete-btn {
  padding: 8px 14px;
  font-size: 0.88rem;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.detail-complete-btn.done {
  background: var(--green);
}

.detail-link-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 8px 14px;
  background: white;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 7px;
  font-weight: 700;
  font-size: 0.88rem;
  text-decoration: none;
}

.detail-link-btn:hover {
  background: var(--paper);
  filter: none;
}

.detail-body {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px 16px;
  overscroll-behavior: contain;
}

.detail-body p {
  margin: 0;
  color: #3e4a40;
  font-size: 0.88rem;
  line-height: 1.8;
}

.login-page {
  display: grid;
  place-items: center;
}

.login-shell {
  width: min(940px, calc(100% - 32px));
  min-height: min(560px, calc(100vh - 32px));
  display: grid;
  align-items: center;
  padding: 48px;
  border-radius: 8px;
  background:
    linear-gradient(110deg, rgba(255, 253, 246, 0.96) 0 48%, rgba(255, 253, 246, 0.45) 48%),
    url("https://images.unsplash.com/photo-1580977276076-ae4b8c219b8e?auto=format&fit=crop&w=1600&q=80") center/cover;
  box-shadow: var(--shadow);
}

.login-panel {
  width: min(420px, 100%);
}

.login-copy {
  color: var(--muted);
  line-height: 1.7;
}

.login-tagline {
  margin: 2px 0 0;
  font-size: 0.88rem;
  color: var(--muted);
  line-height: 1.4;
}

.login-form {
  display: grid;
  gap: 14px;
  margin-top: 24px;
}

.login-form label {
  display: grid;
  gap: 7px;
  font-weight: 800;
}

.form-error {
  margin: 0;
  color: var(--accent-dark);
  font-weight: 800;
}

/* ── Grade selector on login page ── */
.grade-fieldset {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  margin: 0;
}

.grade-fieldset legend {
  padding: 0 6px;
  font-weight: 800;
  font-size: 0.82rem;
  color: var(--muted);
}

.grade-radio {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border: 1.5px solid var(--line);
  border-radius: 7px;
  cursor: pointer;
  margin-top: 5px;
  transition: border-color 0.15s, background 0.15s;
  font-weight: 700;
  font-size: 0.88rem;
}

.grade-radio:has(input:checked) {
  border-color: var(--accent);
  background: rgba(213, 83, 50, 0.07);
}

.grade-radio input[type="radio"] {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
  flex-shrink: 0;
}

.grade-radio span {
  flex: 1;
}

.grade-radio small {
  color: var(--muted);
  font-weight: 400;
  font-size: 0.76rem;
}

@media (max-width: 600px) {
  .spot-list {
    grid-template-columns: 1fr;
  }

  .topbar {
    padding: 6px 12px;
  }

  .search-bar {
    padding: 6px 12px;
  }

  .logout-button {
    padding: 5px 8px;
    font-size: 0.78rem;
  }

  .detail-panel {
    position: fixed;
    left: 10px;
    right: 10px;
    bottom: 10px;
    width: auto;
    /* reset base transform so the panel doesn't shift off-screen */
    transform: none;
    max-height: 54dvh;
  }

  @keyframes slideUp {
    from { transform: translateY(18px); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
  }

  .login-shell {
    width: 100%;
    min-height: 100dvh;
    padding: 28px 22px;
    border-radius: 0;
    background: rgba(255, 253, 246, 0.94);
  }
}

/* ── Admin page ── */

.admin-page {
  min-height: 100dvh;
  padding: 32px 16px 48px;
  background:
    radial-gradient(circle at 80% 10%, rgba(213, 83, 50, 0.1), transparent 30rem),
    linear-gradient(135deg, #f7f5ec 0%, #e8efe6 100%);
}

.admin-shell {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.admin-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.admin-stat-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.admin-stat-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 18px 12px;
  background: white;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(23, 33, 27, 0.06);
}

.admin-stat-value {
  font-size: 2rem;
  font-weight: 900;
  color: var(--accent);
  line-height: 1;
}

.admin-stat-label {
  font-size: 0.82rem;
  color: var(--muted);
}

.admin-table-wrap {
  background: white;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(23, 33, 27, 0.06);
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}

.admin-table thead {
  background: #f1f7ed;
}

.admin-table th {
  padding: 11px 16px;
  text-align: left;
  font-weight: 800;
  color: var(--muted);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  border-bottom: 1px solid var(--line);
}

.admin-table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}

.admin-table tbody tr:last-child td {
  border-bottom: 0;
}

.admin-table tbody tr:hover td {
  background: #fafdf7;
}

.td-name {
  font-weight: 800;
  color: var(--ink);
}

.td-date {
  color: var(--muted);
  font-size: 0.86rem;
  white-space: nowrap;
}

.td-count {
  white-space: nowrap;
}

.count-done {
  font-weight: 800;
  color: var(--green);
}

.count-sep, .count-total {
  color: var(--muted);
}

.count-pct {
  font-size: 0.82rem;
  color: var(--muted);
  margin-left: 4px;
}

.col-bar {
  width: 160px;
}

.td-bar {
  width: 160px;
}

.progress-wrap {
  height: 8px;
  border-radius: 99px;
  background: var(--line);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  border-radius: 99px;
  background: var(--accent);
  transition: width 0.4s ease;
  min-width: 2px;
}

.progress-fill.full {
  background: var(--green);
}

.td-empty {
  text-align: center;
  color: var(--muted);
  padding: 32px;
}

@media (max-width: 540px) {
  .admin-stat-row {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }

  .admin-stat-card {
    padding: 14px 8px;
  }

  .admin-stat-value {
    font-size: 1.6rem;
  }

  .col-bar, .td-bar {
    display: none;
  }
}