:root {
  --bg: #f6f3ee;
  --panel: #fff;
  --ink: #2b1d14;
  --muted: #8a7a6d;
  --line: #ebe4da;
  --gold: #e0a526;
  --brown: #4a2c1a;
  --owned: #1f9d55;
  --missing: #e3ddd4;
  --classic: #f0a92e;
  --alt: #7c5cc4;
  /* cafe status colours (map, list, legend) */
  --st-open: #f0a92e;
  --st-soon: #3b82f6;
  --st-closed: #a8a29e;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--bg);
}
* { box-sizing: border-box; }
body { margin: 0; padding: 0 1.5rem 1.5rem; }

header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 1rem; padding: 1rem 0; }
h1 { margin: 0; color: var(--brown); }
h2 { font-size: 1rem; margin: 1.25rem 0 .5rem; color: var(--brown); }
h2:first-child { margin-top: 0; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }

button, .button {
  font: inherit; cursor: pointer; border: 1px solid var(--brown); background: var(--panel);
  color: var(--brown); padding: .4rem .8rem; border-radius: 6px;
}
button:hover, .button:hover { background: #fbf3e2; }

/* Magnet kind icon (inline SVG) */
.ic { width: 1.15em; height: 1.15em; vertical-align: -0.22em; margin-right: .35em; flex: none; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); gap: .7rem; margin-bottom: 1rem; }
.card { background: var(--panel); border-radius: 10px; padding: .85rem; box-shadow: 0 1px 3px #0001; }
.card .value { font-size: 1.7rem; white-space: nowrap; font-weight: 700; color: var(--brown); display: flex; align-items: center; }
.card .value .ic { width: 1.4rem; height: 1.4rem; margin-right: .4rem; }
.card .label { color: var(--muted); font-size: .85rem; }
.card .value .sdot { width: 14px; height: 14px; margin-right: .5rem; flex: none; }
.card .value .sdot.soon { border-width: 3px; }

main { display: grid; grid-template-columns: 280px 1fr 420px; gap: 1rem; align-items: start; }
@media (max-width: 1100px) { main { grid-template-columns: 1fr; } }
.panel { background: var(--panel); border-radius: 10px; padding: 1rem; box-shadow: 0 1px 3px #0001; }
.map-panel { padding: 0; overflow: hidden; }
#map-canvas { height: 610px; }

/* Mode switch */
.mode { display: flex; align-items: center; font-size: .85rem; }
.mode span { color: var(--muted); margin-right: .5rem; }
.mode button { border-radius: 0; margin-left: -1px; display: inline-flex; align-items: center; }
.mode button:first-of-type { border-radius: 6px 0 0 6px; }
.mode button:last-of-type { border-radius: 0 6px 6px 0; }
.mode button.active { background: var(--brown); color: #fff; }

/* Continents */
.continent { cursor: pointer; margin-bottom: .7rem; }
.continent.active .name { font-weight: 700; }
.continent .row { display: flex; justify-content: space-between; font-size: .9rem; }
.bar { height: 8px; background: var(--missing); border-radius: 4px; overflow: hidden; margin-top: 3px; }
.bar > div { height: 100%; background: var(--owned); }
.bar.alt > div { background: var(--alt); }
.continent .sub { font-size: .75rem; color: var(--muted); margin-top: 3px; display: flex; align-items: center; }
.continent .sub .ic { width: 13px; height: 13px; margin-right: .25rem; }
.continent .sub .sep { width: .6rem; }
.hint { font-size: .75rem; color: var(--muted); margin: .25rem 0 0; }
.side > div + div > h2:first-child { margin-top: 1.25rem; }

#countries, #recent { padding-left: 1.2rem; margin: 0; font-size: .9rem; }
#countries li, #recent li { margin: .15rem 0; }
#recent { list-style: none; padding-left: 0; }
.muted { color: var(--muted); }

/* List */
.filters { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: .5rem; }
.filters input, .filters select { font: inherit; padding: .35rem; flex: 1; min-width: 0; }
.list-head {
  display: grid; grid-template-columns: 1fr 60px 60px 14px; align-items: center;
  font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em;
  border-bottom: 1px solid var(--line); padding: 0 .1rem .35rem;
}
.kind-head { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.kind-head .ic { width: 18px; height: 18px; margin: 0; }
#cafe-list { max-height: 560px; overflow-y: auto; }
#cafe-list h3 { font-size: .95rem; margin: .8rem 0 .25rem; color: var(--brown); position: sticky; top: 0; background: var(--panel); z-index: 1; }
#cafe-list h4 { font-size: .75rem; margin: .5rem 0 .15rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.cafe { display: grid; grid-template-columns: 14px 1fr 60px 60px 14px; align-items: center; gap: .3rem; padding: .12rem .1rem; font-size: .9rem; border-radius: 6px; }
.cafe:hover { background: #faf7f2; }
.cafe .name { min-width: 0; display: flex; align-items: center; flex-wrap: wrap; column-gap: .4rem; }
.cafe .name .nm { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cafe .name .wish-note { margin-left: 0; }
.cafe.is-closed .name .nm { color: var(--muted); }
.cafe .del { border: none; padding: 0 .3rem; color: var(--muted); background: none; }

.sdot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.sdot.open { background: var(--st-open); }
.sdot.soon { background: #fff; border: 2px solid var(--st-soon); }
.sdot.closed { background: var(--st-closed); }

.tag { font-size: .68rem; border-radius: 4px; padding: .05rem .35rem; white-space: nowrap; }
.tag.closed { background: #f1efec; color: var(--muted); }
.tag.soon { background: #e8f0fe; color: #1d4ed8; }
.tag.custom { background: #fbf3e2; color: var(--brown); }

/* Kind toggle buttons: icon-only, faded when not owned */
.kt {
  justify-self: center; display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; border-radius: 50%; cursor: pointer; position: relative;
  border: 1.5px dashed #d6cec3; background: #fff; transition: transform .1s, border-color .1s;
}
.kt input { display: none; }
.kt .ic { width: 20px; height: 20px; margin: 0; filter: grayscale(1); opacity: .3; transition: opacity .1s, filter .1s; }
.kt:hover { border-color: var(--brown); transform: scale(1.06); }
.kt:hover .ic { opacity: .65; }
.kt.on { border: 1.5px solid var(--owned); background: #eef9f2; }
.kt.on .ic { filter: none; opacity: 1; }
.kt.on::after {
  content: "✓"; position: absolute; right: -4px; bottom: -3px; width: 13px; height: 13px; border-radius: 50%;
  background: var(--owned); color: #fff; font-size: 9px; line-height: 13px; text-align: center; font-weight: 700;
}

/* Wishlist */
:root { --wish: #db2777; }
.kcell { display: inline-flex; align-items: center; justify-content: center; gap: 1px; }
.star, .star-slot { width: 18px; flex: none; }
.star {
  border: none; background: none; padding: 0; font-size: 15px; line-height: 1; color: #cfc6ba; cursor: pointer;
}
.star:hover { color: var(--wish); background: none; transform: scale(1.15); }
.star.on { color: var(--wish); }
.cafe.is-wanted { background: #fdf2f8; }
.wish-note {
  display: inline-block; margin-left: .4rem; font-size: .72rem; color: var(--wish); font-style: italic;
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; vertical-align: middle;
}
.star-ic { color: var(--wish); }
.card .value .star-ic { font-size: 1.5rem; margin-right: .4rem; }
#wishlist-items { list-style: none; padding: 0; margin: 0; font-size: .88rem; }
#wishlist-items li[data-id] { display: flex; gap: .2rem; align-items: flex-start; padding: .2rem .25rem; border-radius: 6px; cursor: pointer; }
#wishlist-items li[data-id]:hover { background: #fdf2f8; }
#wishlist-items li .ic { margin-top: .15em; }
#wishlist-items .wish-note { margin-left: 0; }
button.danger { border-color: #b91c1c; color: #b91c1c; margin-right: auto; }
button.danger:hover { background: #fef2f2; }

/* Map markers */
.mk {
  width: 100%; height: 100%; border-radius: 50%; box-sizing: border-box;
  border: 1.5px solid #fff; box-shadow: 0 0 0 1px #0003;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 11px; font-weight: 800; line-height: 1;
}
.mk.open { background: var(--st-open); }
.mk.soon { background: #fff; border: 2.5px solid var(--st-soon); box-shadow: none; }
.mk.closed { background: var(--st-closed); opacity: .85; }
.mk.owned { border: 3px solid var(--owned); box-shadow: 0 0 0 1.5px #fff, 0 1px 4px #0005; opacity: 1; color: #fff; text-shadow: 0 0 2px #0008; }
.mk.soon.owned { color: var(--owned); text-shadow: none; }
.mk.wanted {
  background: var(--wish); border: 2px solid #fff; box-shadow: 0 0 0 1.5px var(--wish), 0 1px 4px #0005;
  color: #fff; font-size: 11px; opacity: 1;
}
.leaflet-tooltip.mk-label.wanted { font-weight: 700; color: var(--wish); }

.leaflet-tooltip.mk-label {
  display: none; background: #fffffff0; border: none; box-shadow: 0 1px 2px #0003;
  padding: 0 .3rem; font-size: 11px; color: var(--ink); border-radius: 3px;
}
.leaflet-tooltip.mk-label::before { display: none; }
.leaflet-tooltip.mk-label.closed { color: var(--muted); }
.leaflet-tooltip.mk-label.soon { color: #1d4ed8; }
.leaflet-tooltip.mk-label.owned { font-weight: 700; color: #166534; }
#map-canvas.show-labels .leaflet-tooltip.mk-label { display: block; }

.legend { display: flex; flex-wrap: wrap; gap: .4rem; padding: .5rem .75rem; font-size: .8rem; align-items: center; }
.lg {
  display: inline-flex; align-items: center; gap: .35rem; border: 1px solid var(--line); background: #fff;
  border-radius: 999px; padding: .15rem .6rem; font-size: .8rem; color: var(--ink);
}
.lg .mk { width: 12px; height: 12px; display: inline-flex; font-size: 8px; }
.lg .mk.owned { width: 16px; height: 16px; border-width: 2.5px; }
.lg.off { opacity: .4; text-decoration: line-through; }
.lg.static { border-color: transparent; cursor: default; }

.pill { font-size: .7rem; border-radius: 999px; padding: .05rem .45rem; color: #fff; }
.pill.open { background: #c98a1b; }
.pill.soon { background: var(--st-soon); }
.pill.closed { background: #78716c; }
.popup-buttons { margin-top: .5rem; display: flex; flex-direction: column; gap: .3rem; }
.popup-row { display: flex; align-items: center; gap: .3rem; }
.popup-buttons button { font-size: .75rem; padding: .2rem .5rem; display: inline-flex; align-items: center; }
.popup-buttons button.on { background: #e9f7ef; border-color: var(--owned); color: #166534; font-weight: 600; }
.popup-buttons button.pstar { border-color: #f3c3da; color: var(--wish); }
.popup-buttons button.pstar.on { background: #fdf2f8; border-color: var(--wish); font-weight: 600; }

/* Help dialog */
#dlg-help { max-width: 640px; }
.kinds { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
@media (max-width: 560px) { .kinds { grid-template-columns: 1fr; } }
.kinds figure { margin: 0; text-align: center; background: var(--bg); border-radius: 10px; padding: .75rem; }
.kind-art { width: 100%; height: 170px; }
.kinds figcaption { text-align: left; font-size: .85rem; }
.kinds figcaption b { display: flex; align-items: center; margin-bottom: .2rem; }

footer { color: var(--muted); font-size: .8rem; margin-top: 1rem; }

dialog { border: none; border-radius: 10px; max-width: 480px; width: 90%; }
dialog::backdrop { background: #0006; }
dialog label { display: block; margin: .5rem 0; }
dialog input, dialog select, dialog textarea { width: 100%; font: inherit; padding: .35rem; }
dialog menu { display: flex; justify-content: flex-end; gap: .5rem; padding: 0; }
#import-result, #export-result { font-size: .85rem; margin: .5rem 0; }
#export-text { font-family: ui-monospace, Menlo, monospace; font-size: .75rem; }

.banner {
  display: flex; align-items: center; gap: .75rem; flex-wrap: wrap;
  background: #fff8e6; border: 1px solid #f0d9a0; border-radius: 10px; padding: .6rem 1rem; margin-bottom: 1rem;
}
.banner[hidden] { display: none; }
.banner button { padding: .25rem .7rem; font-size: .9rem; }

.save-status { align-self: center; font-size: .8rem; color: var(--muted); }
.save-status.ok { color: #166534; }
.save-status.warn { color: #b45309; }

/* ---------- Phone layout ---------- */

.menu-btn, .tabbar, .ph-only { display: none; }
.titlebar { display: contents; }

@media (max-width: 760px) {
  :root { --tabbar-h: 58px; }
  body {
    padding: 0 .75rem calc(var(--tabbar-h) + env(safe-area-inset-bottom) + .75rem);
    padding-left: max(.75rem, env(safe-area-inset-left));
    padding-right: max(.75rem, env(safe-area-inset-right));
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
  }

  /* Only the active tab's sections are shown */
  body[data-view="map"] [data-mview]:not([data-mview~="map"]),
  body[data-view="list"] [data-mview]:not([data-mview~="list"]),
  body[data-view="wishlist"] [data-mview]:not([data-mview~="wishlist"]),
  body[data-view="stats"] [data-mview]:not([data-mview~="stats"]) { display: none !important; }

  /* Header: title + menu button, then the mode switch */
  header {
    position: sticky; top: 0; z-index: 1100; background: var(--bg);
    padding: calc(env(safe-area-inset-top) + .5rem) 0 .5rem; gap: .5rem;
    flex-direction: column; align-items: stretch;
  }
  .titlebar { display: flex; align-items: center; justify-content: space-between; }
  h1 { font-size: 1.35rem; }
  .menu-btn { display: inline-flex; font-size: 1.2rem; padding: .3rem .7rem; line-height: 1; }
  .mode { width: 100%; }
  .mode span { display: none; }
  .mode button { flex: 1; justify-content: center; padding: .45rem .3rem; font-size: .85rem; }

  /* Actions live in a dropdown menu */
  .actions {
    display: none; position: absolute; right: 0; top: calc(env(safe-area-inset-top) + 2.8rem);
    flex-direction: column; align-items: stretch; gap: .35rem; min-width: 220px;
    background: var(--panel); border-radius: 10px; padding: .6rem; box-shadow: 0 6px 24px #0003;
  }
  .actions.open { display: flex; }
  .actions button, .actions .button { text-align: left; padding: .6rem .8rem; }
  .save-status { padding: .2rem .3rem 0; }

  .ph-hide { display: none; }
  .ph-only { display: inline; }
  .banner { padding: .45rem .75rem; font-size: .88rem; gap: .5rem; margin-bottom: .6rem; flex-wrap: nowrap; justify-content: space-between; }
  .banner > span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .banner button { flex: none; }
  .banner button { padding: .3rem .6rem; font-size: .85rem; }

  /* Stats cards: one swipeable row */
  .cards {
    display: flex; overflow-x: auto; scroll-snap-type: x mandatory; gap: .6rem;
    margin: 0 -.75rem .75rem; padding: 0 .75rem .25rem; -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  .cards::-webkit-scrollbar { display: none; }
  .card { flex: 0 0 auto; min-width: 128px; scroll-snap-align: start; padding: .7rem .8rem; }
  .card .value { font-size: 1.45rem; }
  .card .label { font-size: .78rem; }

  main { display: block; }
  .panel { margin-bottom: .75rem; border-radius: 12px; }

  /* Map fills the screen between header and tab bar */
  .map-panel { margin: 0 -.75rem; border-radius: 0; box-shadow: none; }
  #map-canvas { height: calc(100dvh - var(--above-map, 120px) - var(--tabbar-h) - env(safe-area-inset-bottom) - 64px); min-height: 320px; }
  .legend { padding: .45rem .75rem; gap: .3rem; }
  .lg { font-size: .74rem; padding: .15rem .5rem; }
  .lg.static.muted { display: none; }

  /* List: page scrolls, bigger touch targets */
  #cafe-list { max-height: none; overflow: visible; }
  #cafe-list h3 { top: var(--header-h, 120px); padding: .3rem 0; }
  .filters { flex-wrap: wrap; }
  .filters input { flex: 1 1 100%; }
  .filters select { flex: 1 1 40%; }
  .cafe { grid-template-columns: 12px 1fr 64px 64px 10px; padding: .3rem .1rem; font-size: .95rem; }
  .list-head { grid-template-columns: 1fr 64px 64px 10px; }
  .kt { width: 36px; height: 36px; }
  .kt .ic { width: 24px; height: 24px; }
  .star, .star-slot { width: 24px; }
  .star { font-size: 18px; padding: 6px 0; }

  #wishlist-items li[data-id] { padding: .45rem .3rem; font-size: .95rem; }
  body[data-view="wishlist"] .side h2 { margin-top: 0; }

  /* iOS zooms into inputs with text smaller than 16px */
  input, select, textarea { font-size: 16px !important; }

  dialog { width: 94vw; max-width: none; margin: auto; padding: 1rem; }
  dialog menu { flex-wrap: wrap; }
  .kind-art { height: 140px; }

  footer { margin-bottom: .5rem; }

  /* Bottom tab bar */
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 1200;
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    background: #fffffff2; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    border-top: 1px solid var(--line);
  }
  .tabbar button {
    flex: 1; border: none; background: none; border-radius: 0; padding: .35rem 0 0;
    display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: .7rem; color: var(--muted);
  }
  .tabbar button:hover { background: none; }
  .tabbar .tb-ic { font-size: 1.25rem; line-height: 1.2; filter: grayscale(1); opacity: .6; }
  .tabbar .tb-star { color: var(--wish); filter: none; }
  .tabbar button.active { color: var(--brown); font-weight: 700; }
  .tabbar button.active .tb-ic { filter: none; opacity: 1; }
}
