:root {
  --bg: #f5f3ee;
  --surface: #ffffff;
  --surface-2: #f3f1eb;
  --ink: #1c2530;
  --muted: #647084;
  --line: #e2ded5;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #d8efea;
  --danger: #b42318;
  --c-note: #55647a;
  --c-event: #bd2460;
  --c-flight: #1d5fd0;
  --c-train: #6d3fc9;
  --c-hotel: #a85a0a;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 30, 40, .06), 0 10px 30px rgba(20, 30, 40, .12);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10141a;
    --surface: #1a2029;
    --surface-2: #222a35;
    --ink: #e8edf3;
    --muted: #98a4b5;
    --line: #2d3644;
    --accent: #2cc5b3;
    --accent-ink: #05211e;
    --accent-soft: #163a3b;
    --danger: #ff8073;
    --c-note: #a3b0c2;
    --c-event: #f47fae;
    --c-flight: #77aaff;
    --c-train: #b59bff;
    --c-hotel: #f1ad5c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .45);
  }
}
.t-note { --tc: var(--c-note); }
.t-event { --tc: var(--c-event); }
.t-flight { --tc: var(--c-flight); }
.t-train { --tc: var(--c-train); }
.t-hotel { --tc: var(--c-hotel); }

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  height: 100dvh;
  overflow: hidden;
  display: grid;
  grid-template: "title tabs actions" auto "main main main" minmax(0, 1fr) / auto 1fr auto;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
[hidden] { display: none !important; }
.spacer { flex: 1; }
.hint { color: var(--muted); font-size: 14px; }
.empty { color: var(--muted); padding: 28px 8px; text-align: center; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- top bar ---------- */
.brand, .tabs, .top-actions {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
}
.brand { grid-area: title; font-size: 18px; font-weight: 700; letter-spacing: -.01em; padding-left: calc(16px + env(safe-area-inset-left)); white-space: nowrap; }
.tabs { grid-area: tabs; gap: 4px; }
.top-actions { grid-area: actions; gap: 8px; padding-right: calc(16px + env(safe-area-inset-right)); }
main { grid-area: main; min-height: 0; position: relative; }

.tab {
  display: flex; align-items: center; gap: 7px;
  border: 0; background: none; color: var(--muted);
  padding: 8px 14px; border-radius: 10px; font-weight: 600;
}
.tab svg, .icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tab:hover { background: var(--surface-2); }
.tab.active { color: var(--accent); background: var(--accent-soft); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 16px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); color: var(--ink);
  font-weight: 600; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn.tiny { min-height: 26px; padding: 2px 8px; font-size: 12px; margin-left: 6px; }
.icon-btn {
  width: 40px; height: 40px; flex: none;
  display: inline-grid; place-items: center;
  border: 0; border-radius: 10px; background: none; color: var(--muted); font-size: 20px; line-height: 1;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.btn:disabled { opacity: .6; cursor: default; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.warn { color: var(--danger); font-size: 14px; font-weight: 600; }
.setup-qr { width: 220px; }
.secret { font: 600 15px/1.6 ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: .04em; overflow-wrap: anywhere; user-select: all; }

/* sync state dot on the settings button */
#btn-settings { position: relative; }
#btn-settings::after {
  content: ''; position: absolute; top: 7px; right: 7px; width: 9px; height: 9px; border-radius: 50%;
  border: 2px solid var(--surface); background: transparent; display: none;
}
#btn-settings[data-sync=idle]::after { display: block; background: #1f9d55; }
#btn-settings[data-sync=syncing]::after { display: block; background: var(--accent); animation: pulse 1s ease-in-out infinite; }
#btn-settings[data-sync=offline]::after { display: block; background: #d99a06; }
#btn-settings[data-sync=error]::after, #btn-settings[data-sync=signed-out]::after { display: block; background: var(--danger); }
@keyframes pulse { 50% { opacity: .25; } }
@media (prefers-reduced-motion: reduce) { #btn-settings::after { animation: none !important; } }

/* ---------- views ---------- */
.view { height: 100%; overflow: auto; -webkit-overflow-scrolling: touch; }

.view-calendar { display: grid; grid-template-columns: minmax(0, 1fr) 380px; overflow: hidden; }
.cal { display: flex; flex-direction: column; min-height: 0; padding: 16px 20px 20px; }
.cal-bar { display: flex; align-items: center; gap: 4px; margin-bottom: 10px; }
.cal-bar h2 { font-size: 22px; letter-spacing: -.02em; min-width: 190px; text-align: center; }
.weekdays { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); padding: 0 1px 6px; }
.weekdays span { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding-left: 8px; }
.grid {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); grid-auto-rows: minmax(0, 1fr);
  gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
}
.day {
  border: 0; border-radius: 0; background: var(--surface); text-align: left;
  display: flex; flex-direction: column; gap: 3px; padding: 6px; min-width: 0; min-height: 0; overflow: hidden;
}
.day:hover { background: var(--surface-2); }
.day.other { background: var(--bg); }
.day.other .num { color: var(--muted); opacity: .7; }
.day.selected { box-shadow: inset 0 0 0 2px var(--accent); }
.day:focus-visible { outline-offset: -3px; }
.num { width: 26px; height: 26px; flex: none; display: grid; place-items: center; border-radius: 50%; font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }
.today .num { background: var(--accent); color: var(--accent-ink); }
.chips { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.chip {
  font-size: 12px; line-height: 1.5; padding: 0 6px; border-radius: 5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  background: color-mix(in srgb, var(--tc) 15%, transparent);
  border-left: 3px solid var(--tc);
}
.more { font-size: 11px; color: var(--muted); padding-left: 6px; }
.dots { display: none; gap: 3px; justify-content: center; height: 6px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--tc); }

.day-panel { border-left: 1px solid var(--line); background: var(--surface); overflow: auto; padding: 16px; }
.day-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.day-head h2 { font-size: 17px; flex: 1; }

.card {
  width: 100%; display: flex; gap: 12px; align-items: flex-start; text-align: left;
  padding: 12px; margin-bottom: 8px;
  background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--tc); border-radius: 12px;
}
.card:hover { background: var(--surface-2); }
.card-icon { font-size: 22px; line-height: 1.2; }
.card-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
.card-main strong { overflow-wrap: anywhere; }
.card-sub { font-size: 14px; overflow-wrap: anywhere; }
.card-meta { font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }

.list-wrap { max-width: 720px; margin: 0 auto; padding: 16px; }
.list-date { font-size: 14px; font-weight: 700; margin: 20px 0 8px; scroll-margin-top: 12px; }
.list-date.past { color: var(--muted); }

.view-map { display: flex; flex-direction: column; overflow: hidden; }
.map-bar { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line); }
#map { flex: 1; min-height: 0; z-index: 0; }
.leaflet-container { font: inherit; background: var(--surface-2); }
.pin {
  width: 36px; height: 36px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  background: #fff; border: 3px solid var(--tc); box-shadow: 0 2px 6px rgba(0, 0, 0, .35);
  display: grid; place-items: center;
}
.pin span { transform: rotate(45deg); font-size: 16px; line-height: 1; }
.popup { display: flex; flex-direction: column; gap: 3px; min-width: 170px; font-size: 14px; color: #1c2530; }
.popup strong { font-size: 15px; }
.popup span:empty { display: none; }
.popup .btn { margin-top: 6px; align-self: flex-start; }

/* ---------- form controls ---------- */
input:not([type=radio]):not([type=file]), select, textarea {
  width: 100%; min-height: 42px; padding: 8px 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
}
textarea { resize: vertical; min-height: 96px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.map-bar select { width: auto; }
.fld { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.fld > span { font-size: 13px; font-weight: 600; color: var(--muted); }
.row3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.row2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.row2:empty { display: none; }
.inline { display: flex; gap: 8px; align-items: flex-end; }
.inline > input, .grow { flex: 1; min-width: 0; }
.group { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin: 0; min-width: 0; display: flex; flex-direction: column; gap: 10px; }
.group legend { font-weight: 700; font-size: 14px; padding: 0 6px; }
.group p { font-size: 14px; }

.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg label { flex: 1 1 auto; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: block; text-align: center; padding: 8px 10px; white-space: nowrap;
  border: 1px solid var(--line); border-radius: 10px; font-weight: 600; font-size: 14px; cursor: pointer;
}
.seg input:checked + span { border-color: var(--tc); background: color-mix(in srgb, var(--tc) 15%, transparent); box-shadow: inset 0 0 0 1px var(--tc); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.link-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.6fr) auto; gap: 8px; margin-bottom: 8px; }
.result { display: block; width: 100%; text-align: left; padding: 10px 12px; border: 0; border-bottom: 1px solid var(--line); background: none; font-size: 14px; }
.result:hover { background: var(--surface-2); }
.mini-map { height: 190px; border-radius: 12px; border: 1px solid var(--line); overflow: hidden; z-index: 0; }
.mini-map.tall { height: 260px; }

.att-row { display: flex; align-items: center; gap: 12px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.att-row:last-child { border-bottom: 0; }
.att-thumb { width: 52px; height: 52px; flex: none; border-radius: 8px; overflow: hidden; background: var(--surface-2); display: grid; place-items: center; }
.att-thumb.qr { background: #fff; }
.att-thumb img, .att-thumb svg { width: 100%; height: 100%; object-fit: cover; display: block; }
.att-name { flex: 1; min-width: 0; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-name strong { display: block; }
.file-icon { font-size: 24px; }

/* ---------- sheets (dialogs) ---------- */
dialog { border: 0; padding: 0; color: var(--ink); }
dialog::backdrop { background: rgba(10, 16, 24, .5); }
.sheet {
  width: min(680px, calc(100vw - 32px)); max-height: min(880px, calc(100dvh - 32px));
  background: var(--surface); border-radius: 18px; box-shadow: var(--shadow); overflow: hidden;
}
.sheet[open] { display: flex; flex-direction: column; }
.sheet-form { display: contents; }
.sheet-head { display: flex; align-items: center; gap: 12px; padding: 14px 12px 14px 20px; border-bottom: 1px solid var(--line); }
.sheet-head.t-note, .sheet-head.t-event, .sheet-head.t-flight, .sheet-head.t-train, .sheet-head.t-hotel { border-top: 5px solid var(--tc); }
.sheet-title { flex: 1; min-width: 0; }
.sheet-title small { color: var(--tc, var(--muted)); font-weight: 700; text-transform: uppercase; letter-spacing: .06em; font-size: 12px; }
.sheet-title h2 { font-size: 20px; letter-spacing: -.01em; overflow-wrap: anywhere; }
.type-badge { font-size: 28px; }
.sheet-body { flex: 1; min-height: 0; overflow: auto; padding: 18px 20px; display: flex; flex-direction: column; gap: 16px; -webkit-overflow-scrolling: touch; }
.sheet-body > * { flex: none; }
.sheet-foot { display: flex; gap: 8px; padding: 12px 20px; padding-bottom: calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.sheet-body section { display: flex; flex-direction: column; gap: 8px; }
.sheet-body h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.when { font-weight: 600; }
.fields { margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px 16px; }
.fields dt { font-size: 13px; color: var(--muted); }
.fields dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
.notes { white-space: pre-wrap; overflow-wrap: anywhere; }
.links { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.links a { display: block; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; text-decoration: none; overflow-wrap: anywhere; }
.links a:hover { background: var(--surface-2); }
.loc-name { font-weight: 600; }
.qr-row { display: flex; flex-wrap: wrap; gap: 12px; }
.qr-card { background: #fff; color: #1c2530; border: 1px solid var(--line); border-radius: 14px; padding: 12px; width: 200px; display: flex; flex-direction: column; gap: 8px; align-items: center; font-size: 12px; }
.qr-card img, .qr-card svg { width: 100%; height: auto; display: block; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; }
.thumb { aspect-ratio: 1; border: 0; padding: 0; border-radius: 10px; overflow: hidden; background: var(--surface-2); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

#lightbox { width: 100vw; height: 100dvh; max-width: none; max-height: none; background: #000; }
#lightbox.light { background: #fff; color: #1c2530; }
.lb-content { width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; padding: 24px; }
.lb-content img { max-width: 100%; max-height: 100%; object-fit: contain; min-height: 0; }
.lb-content svg { width: min(86vw, 70dvh); height: auto; }
.lb-content p { overflow-wrap: anywhere; text-align: center; font-size: 14px; max-width: 60ch; }
.lb-close { position: absolute; top: calc(12px + env(safe-area-inset-top)); right: 12px; background: rgba(128, 128, 128, .35); color: #fff; }
#lightbox.light .lb-close { color: #1c2530; }

#toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translate(-50%, 12px);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 2147483647; max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (prefers-reduced-motion: reduce) { #toast { transition: none; } }

/* ---------- tablets in portrait ---------- */
@media (max-width: 1023px) {
  .view-calendar { grid-template-columns: minmax(0, 1fr) 320px; }
}

/* ---------- phones ---------- */
@media (max-width: 719px) {
  body { grid-template: "title actions" auto "main main" minmax(0, 1fr) "tabs tabs" auto / 1fr auto; }
  .tabs {
    border-bottom: 0; border-top: 1px solid var(--line); justify-content: space-around;
    padding: 6px calc(8px + env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) calc(8px + env(safe-area-inset-left));
  }
  .tab { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 18px; }
  .tab.active { background: none; }
  .tab svg { width: 24px; height: 24px; }

  .view-calendar { display: block; overflow: auto; }
  .cal { padding: 10px 10px 12px; }
  .cal-bar h2 { font-size: 19px; min-width: 150px; }
  .weekdays span { padding: 0; text-align: center; font-size: 11px; }
  .grid { flex: none; grid-auto-rows: 52px; }
  .day { align-items: center; justify-content: center; padding: 4px 0; gap: 4px; }
  .chips { display: none; }
  .dots { display: flex; }
  .day-panel { border-left: 0; border-top: 1px solid var(--line); overflow: visible; min-height: 220px; }

  .sheet { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; }
  .sheet-head { padding-top: calc(14px + env(safe-area-inset-top)); }
  .seg { flex-wrap: nowrap; gap: 4px; }
  .seg label { flex: 1 1 0; min-width: 0; }
  .seg span { white-space: normal; padding: 6px 2px; font-size: 12px; }
  .fields { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .row3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .link-row { grid-template-columns: minmax(0, 1fr) auto; }
  .link-row [data-l=label] { grid-column: 1 / -1; }
  .qr-card { width: min(100%, 260px); }
}
