:root {
  --land: #F4C263;
  --bar: #F0B24F;
  --panel: #FFFFFF;
  --pill: #FBEFD0;
  --ink: 0, 0, 0;
  --btn: #2F3A4A;
  --btn-text: #FFFFFF;
  --line: rgba(0, 0, 0, 0.14);
  --scrim: rgba(244, 194, 99, 0.5);
  --danger: #B3261E;
  --bar-h: calc(env(safe-area-inset-top, 0px) + 52px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --display: 'Special Gothic Expanded One', 'Arial Black', sans-serif;
  --body: 'Special Gothic', 'Avenir Next', system-ui, sans-serif;
}
:root[data-theme='dark'] {
  --land: #1B2130;
  --bar: #141925;
  --panel: #232B3D;
  --pill: #2C3549;
  --ink: 255, 255, 255;
  --btn: #E8ECF3;
  --btn-text: #141925;
  --line: rgba(255, 255, 255, 0.16);
  --scrim: rgba(27, 33, 48, 0.5);
  --danger: #FF8A80;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; overflow: hidden; overscroll-behavior: none; }
body { font-family: var(--body); font-size: 16px; color: rgb(var(--ink)); background: var(--land); }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid #3E9BD8; outline-offset: 2px; }

#map { position: fixed; inset: 0; background: var(--land); }
.maplibregl-ctrl-top-right { top: calc(var(--bar-h) + 6px); }

/* Top block: masks the phone status bar and names the city */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; height: var(--bar-h); z-index: 20;
  padding-top: env(safe-area-inset-top, 0px);
  background: var(--bar); display: flex; align-items: center; justify-content: center;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.12);
}
#cityBtn {
  min-height: 44px; max-width: 80%; padding: 0 12px; border: 0; background: none;
  display: flex; align-items: center; gap: 8px;
  font-family: var(--display); font-size: 19px;
}
#cityName { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

#menuBtn {
  position: fixed; top: calc(var(--bar-h) + 14px); left: 14px; z-index: 10;
  width: 46px; height: 46px; border-radius: 50%; border: 0;
  background: var(--btn); color: var(--btn-text);
  display: grid; place-items: center; box-shadow: 3px 4px 0 rgba(0, 0, 0, 0.2);
}

/* Panels: city list and menu */
#scrim { position: fixed; inset: var(--bar-h) 0 0 0; z-index: 30; background: var(--scrim); }
.panel {
  position: fixed; z-index: 31; background: var(--panel); border-radius: 18px;
  box-shadow: 4px 6px 0 rgba(0, 0, 0, 0.2); overflow: hidden auto;
  max-height: calc(100dvh - var(--bar-h) - 40px);
}
#cityPanel { top: calc(var(--bar-h) + 10px); left: 32px; right: 32px; }
#menuPanel { top: calc(var(--bar-h) + 14px); left: 14px; width: min(320px, calc(100vw - 28px)); padding: 10px 18px 20px; }
.row {
  width: 100%; min-height: 54px; padding: 0 18px; border: 0; border-bottom: 1px solid var(--line);
  background: none; text-align: left; display: flex; align-items: center; gap: 12px;
}
.row:last-child { border-bottom: 0; }
.row.on { background: rgba(var(--ink), 0.07); font-weight: 700; }
.row .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row small, .sub { font-size: 13px; color: rgba(var(--ink), 0.65); }
.row.strong { font-weight: 700; }
.pad { padding: 12px 18px; }
.mini { min-width: 44px; min-height: 44px; border: 0; background: none; font-size: 13px; color: rgba(var(--ink), 0.65); text-decoration: underline; }
.head { display: flex; align-items: center; justify-content: space-between; }
h2 { margin: 0; font-family: var(--display); font-size: 18px; font-weight: 400; }
.label { display: block; margin: 16px 0 6px; font-size: 12px; font-weight: 700; letter-spacing: 0.06em; color: rgba(var(--ink), 0.65); text-transform: uppercase; }
.x { width: 44px; height: 44px; margin-right: -12px; border: 0; background: none; display: grid; place-items: center; }

.field {
  width: 100%; height: 48px; padding: 0 14px; border: 2px solid rgba(var(--ink), 0.8); border-radius: 12px;
  background: transparent; font-size: 16px;
}
textarea.field { height: 140px; padding: 12px 14px; resize: none; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; border: 2px solid rgba(var(--ink), 0.8); border-radius: 12px; overflow: hidden; }
.seg button, .seg label { height: 46px; border: 0; background: none; display: grid; place-items: center; font-size: 14px; border-right: 2px solid rgba(var(--ink), 0.8); cursor: pointer; }
.seg > :last-child { border-right: 0; }
.seg .on, .seg input:checked + span { background: var(--btn); color: var(--btn-text); font-weight: 700; }
.seg label { position: relative; }
.seg label input { position: absolute; opacity: 0; inset: 0; }
.seg label span { width: 100%; height: 100%; display: grid; place-items: center; }
.seg label input:focus-visible + span { outline: 3px solid #3E9BD8; outline-offset: -3px; }
.btn { height: 48px; padding: 0 24px; border: 0; border-radius: 24px; background: var(--btn); color: var(--btn-text); font-weight: 700; }
.btn.wide { width: 100%; margin-top: 10px; }
.btn.ghost { background: none; color: rgb(var(--ink)); border: 2px solid rgba(var(--ink), 0.8); }
.btn.danger { background: none; color: var(--danger); border: 2px solid var(--danger); }
.swatches { display: flex; flex-wrap: wrap; gap: 4px; margin: 10px 0; }
.swatch { width: 44px; height: 44px; border: 0; background: none; display: grid; place-items: center; }
.swatch span { width: 28px; height: 28px; border-radius: 50%; background: var(--c); border: 3px solid var(--panel); box-shadow: 0 0 0 2px transparent; }
.swatch.on span { box-shadow: 0 0 0 3px rgb(var(--ink)); }
.dotc { width: 14px; height: 14px; border-radius: 50%; background: var(--c); flex: none; }

/* Bottom sheet */
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  height: calc(92px + var(--safe-b)); padding: 0 14px;
  display: flex; flex-direction: column;
  border-radius: 22px 22px 0 0; transition: height 0.25s ease, background 0.2s;
}
#sheet[data-state='open'] { height: min(62dvh, 560px); background: var(--panel); box-shadow: 0 -4px 0 rgba(0, 0, 0, 0.14); }
#sheet[data-state='search'] {
  top: calc(var(--vtop, 0px) + var(--bar-h) + 16px); bottom: auto;
  height: calc(var(--vh, 100dvh) - var(--bar-h) - 16px);
  background: var(--panel); box-shadow: 0 -4px 0 rgba(0, 0, 0, 0.14); transition: none;
}
body.kb #sheet[data-state='search'] { top: calc(var(--vtop, 0px) + 8px); height: calc(var(--vh, 100dvh) - 8px); z-index: 22; }
#sheet[data-state='collapsed'] #sheetBody { display: none; }
body.card-open #sheet, body.card-open #menuBtn { display: none; }
#grab { width: 100%; height: 28px; border: 0; background: none; display: grid; place-items: center; flex: none; touch-action: none; }
#grab span { width: 44px; height: 5px; border-radius: 3px; background: var(--btn); opacity: 0.75; }
.searchrow { display: flex; align-items: center; gap: 6px; flex: none; }
.search {
  flex: 1; min-width: 0; height: 52px; padding: 0 18px; border-radius: 26px; background: var(--pill);
  display: flex; align-items: center; gap: 10px; box-shadow: 3px 4px 0 rgba(0, 0, 0, 0.16);
}
#sheet:not([data-state='collapsed']) .search { box-shadow: none; }
:root[data-theme='light'] #sheet:not([data-state='collapsed']) .search { background: rgba(0, 0, 0, 0.07); }
#q { flex: 1; min-width: 0; height: 100%; border: 0; outline: 0; background: none; font-size: 16px; }
#q::placeholder { color: rgba(var(--ink), 0.6); }
#filterDot { width: 12px; height: 12px; border-radius: 50%; background: #E4572E; border: 2px solid var(--pill); flex: none; }
#cancelSearch { min-height: 44px; padding: 0 8px; border: 0; background: none; font-size: 15px; }
#sheetBody { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 0 calc(16px + var(--safe-b)); -webkit-overflow-scrolling: touch; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 14px 4px; margin: 0 -14px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  height: 44px; padding: 0 14px; border-radius: 22px; border: 2px solid rgba(var(--ink), 0.8); background: none;
  display: flex; align-items: center; gap: 8px; font-size: 14px; white-space: nowrap; flex: none;
}
.chip.on { background: var(--btn); color: var(--btn-text); border-color: var(--btn); font-weight: 700; }
.count { display: flex; align-items: center; justify-content: space-between; min-height: 44px; margin-top: 8px; border-bottom: 1px solid var(--line); font-size: 13px; color: rgba(var(--ink), 0.65); }
.item { width: 100%; min-height: 62px; padding: 8px 0; border: 0; border-bottom: 1px solid var(--line); background: none; text-align: left; display: flex; align-items: center; gap: 12px; }
.item b { display: block; font-size: 16px; }
.item .ico { width: 30px; flex: none; display: grid; place-items: center; }
.empty { padding: 22px 4px; font-size: 14px; color: rgba(var(--ink), 0.65); }

/* Pins */
.pin { width: 44px; height: 44px; padding: 0; border: 0; background: none; display: grid; place-items: center; cursor: pointer; }
.dot { width: 22px; height: 22px; border-radius: 50%; background: var(--c); border: 3px solid #fff; box-shadow: 3px 4px 0 rgba(0, 0, 0, 0.25); transition: transform 0.15s; }
.want .dot, .dot.want { background: #fff; border: 6px solid var(--c); }
.star { width: 34px; height: 34px; filter: drop-shadow(3px 4px 0 rgba(0, 0, 0, 0.25)); transition: transform 0.15s; }
.star path { fill: var(--c); stroke: #fff; stroke-width: 2; stroke-linejoin: round; }
.pin.sel { z-index: 3; }
.pin.sel .dot, .pin.sel .star { transform: scale(1.35); }
.ico .dot { width: 18px; height: 18px; box-shadow: none; border-color: var(--panel); }
.ico .dot.want { border: 5px solid var(--c); background: var(--panel); }
.ico .star { width: 26px; height: 26px; filter: none; }
.ico .star path { stroke: var(--panel); }
#mapScrim { position: absolute; inset: 0; z-index: 2; background: var(--scrim); }

/* Pin card */
#card {
  position: fixed; left: 12px; right: 12px; bottom: calc(18px + var(--safe-b)); z-index: 25;
  background: var(--panel); border-radius: 20px; padding: 14px 20px 20px;
  box-shadow: 4px 6px 0 rgba(0, 0, 0, 0.2); max-height: 60dvh; overflow-y: auto;
}
#card h2 { font-size: 22px; line-height: 1.15; color: rgb(var(--ink)); overflow-wrap: anywhere; }
#card .meta { display: flex; justify-content: space-between; gap: 12px; margin-top: 8px; padding-bottom: 10px; border-bottom: 1px solid var(--line); font-weight: 700; font-size: 15px; color: rgba(var(--ink), 0.9); }
#card .meta span { display: flex; align-items: center; gap: 8px; }
#card .note { margin: 12px 0 0; font-size: 15px; line-height: 1.5; color: rgba(var(--ink), 0.62); white-space: pre-wrap; overflow-wrap: anywhere; }
#card .btn { margin-top: 16px; height: 44px; }

/* Add or edit form */
#form { position: fixed; inset: var(--vtop, 0px) 0 auto 0; height: var(--vh, 100dvh); z-index: 40; background: var(--panel); display: flex; flex-direction: column; }
#form .bar { height: var(--bar-h); padding: env(safe-area-inset-top, 0px) 8px 0; background: var(--bar); display: flex; align-items: center; justify-content: space-between; box-shadow: 0 3px 0 rgba(0, 0, 0, 0.12); flex: none; }
#form .bar button { min-height: 44px; padding: 0 10px; border: 0; background: none; font-size: 16px; }
#form .bar .save { font-weight: 700; }
#form .body { flex: 1; overflow-y: auto; padding: 4px 16px calc(24px + var(--safe-b)); }
.suggest { border: 2px solid rgba(var(--ink), 0.8); border-top: 0; border-radius: 0 0 12px 12px; margin-top: -8px; padding-top: 6px; overflow: hidden; }
.suggest .row { min-height: 46px; padding: 0 14px; }

#toast {
  position: fixed; left: 50%; top: calc(var(--bar-h) + 14px); transform: translateX(-50%); z-index: 60;
  max-width: calc(100vw - 100px); padding: 12px 18px; border-radius: 14px;
  background: var(--btn); color: var(--btn-text); font-size: 14px; text-align: center; box-shadow: 3px 4px 0 rgba(0, 0, 0, 0.2);
}

@media (min-width: 700px) {
  #sheet { left: 16px; right: auto; width: 400px; }
  #card { left: 16px; right: auto; width: 400px; }
  #cityPanel { left: 50%; right: auto; width: 340px; transform: translateX(-50%); }
  #form { left: 50%; right: auto; width: 440px; transform: translateX(-50%); box-shadow: 0 0 0 100vmax var(--scrim); }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
