:root {
  --bg: #f6f4ee;
  --panel: #fffdfa;
  --ink: #242424;
  --muted: #6d675e;
  --line: #d7d0c3;
  --soft: #ebe6da;
  --accent: #2f6f73;
  --danger: #8d2c21;
  --shadow: 0 10px 24px rgba(28, 25, 20, .08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-width: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.55;
}

button, input, select, textarea { font: inherit; }

button {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  padding: 8px 12px;
  cursor: pointer;
  touch-action: manipulation;
}

button:hover { background: var(--soft); }
button:disabled { cursor: not-allowed; opacity: .5; }
button.primary, .segmented button.active {
  border-color: var(--ink);
  background: var(--ink);
  color: #fffdfa;
}
button.danger {
  border-color: rgba(141, 44, 33, .45);
  color: var(--danger);
  background: #fff8f6;
}
button.danger:hover { background: #fff0ec; }

input, select, textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 10px 12px;
}

textarea { resize: vertical; }
label { display: grid; gap: 6px; color: var(--muted); font-size: 13px; }
details summary { cursor: pointer; color: var(--accent); font-size: 13px; }

.app-shell {
  display: flex;
  min-height: 100svh;
  flex-direction: column;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px 18px;
  align-items: end;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(246, 244, 238, .94);
  backdrop-filter: blur(12px);
}

.title-block { min-width: 0; }
.kicker { margin: 0; color: var(--accent); font-size: 12px; font-weight: 700; }
.app-header h1 { margin: 0; font-size: 22px; line-height: 1.15; letter-spacing: 0; }
.app-header p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }

.tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(64px, 1fr));
  gap: 8px;
}

.tab.active { border-color: var(--ink); background: var(--ink); color: #fff; }

.connection {
  grid-column: 1 / -1;
  justify-self: start;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 9px;
  color: var(--muted);
  background: var(--panel);
  font-size: 12px;
}
.connection.online { color: var(--accent); border-color: rgba(47, 111, 115, .35); }
.connection.local { color: var(--danger); border-color: rgba(141, 44, 33, .25); }

main {
  min-height: 0;
  padding: 16px;
}
.page { display: grid; gap: 16px; }

.panel {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 14px;
  box-shadow: var(--shadow);
}

.panel h2, .panel h3 { margin: 0 0 10px; line-height: 1.25; }
.panel h2 { font-size: 17px; }
.panel h3 { font-size: 15px; }
.note { margin: 0; color: var(--muted); font-size: 13px; }

.simple-form {
  display: grid;
  gap: 14px;
  max-width: 760px;
  margin: 0 auto;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.main-input {
  min-height: 220px;
  font-size: 17px;
}

.media-field { display: grid; gap: 12px; }

.big-picker {
  display: grid;
  min-height: 170px;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--accent);
  font-size: 17px;
}
.big-picker input { display: none; }

.media-preview {
  display: block;
  width: 100%;
  max-height: 420px;
  border-radius: 8px;
  object-fit: contain;
  background: #fff;
}

.camera-box {
  display: grid;
  gap: 8px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #111;
}
.camera-preview {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  max-height: 62svh;
  object-fit: cover;
}
.camera-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 8px;
  background: var(--paper);
}

.record-button {
  min-height: 140px;
  border-style: dashed;
  color: var(--accent);
  font-size: 22px;
}
.record-button.active {
  border-color: var(--danger);
  background: #fff2ef;
  color: var(--danger);
}
.audio-preview { width: 100%; }

.subtle-details {
  display: grid;
  gap: 10px;
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

.check-row {
  display: flex;
  gap: 8px;
  align-items: center;
}
.check-row input { width: auto; }

.manual-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.submit-button { min-height: 52px; font-size: 18px; }

.edit-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 16px;
  align-items: start;
}

.side-panel {
  display: grid;
  gap: 10px;
  max-height: calc(100svh - 136px);
  overflow: auto;
}

.record-list, .nearby-list {
  display: grid;
  gap: 8px;
}

.record-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 4px 9px;
  align-items: center;
  width: 100%;
  text-align: left;
  background: #fff;
}
.record-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(47, 111, 115, .16);
}
.record-card strong {
  min-width: 0;
  overflow-wrap: anywhere;
  line-height: 1.25;
}
.record-card small {
  grid-column: 2;
  color: var(--muted);
}
.record-card audio {
  grid-column: 1 / -1;
  width: 100%;
}
.record-kind {
  display: grid;
  width: 30px;
  height: 30px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: 12px;
}

.canvas-panel { padding: 0; overflow: hidden; }
.canvas-toolbar, .map-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
}

.small-action { min-height: 34px; padding: 5px 12px; }

.edit-canvas {
  position: relative;
  height: min(72svh, 760px);
  min-height: 560px;
  overflow: hidden;
  touch-action: none;
  cursor: grab;
  background:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px),
    #faf8f1;
  background-size: 40px 40px;
}
.edit-canvas:active { cursor: grabbing; }
.edit-world {
  position: absolute;
  inset: 0 auto auto 0;
  width: 2400px;
  height: 1600px;
  transform-origin: 0 0;
  will-change: transform;
}
.edge-layer {
  position: absolute;
  inset: 0;
  width: 2400px;
  height: 1600px;
  pointer-events: none;
}
.record-node {
  position: absolute;
  z-index: 2;
  display: grid;
  gap: 4px;
  width: 176px;
  min-height: 96px;
  border: 2px solid var(--ink);
  background: rgba(255,253,250,.96);
  text-align: left;
  touch-action: none;
}
.record-node.selected { outline: 4px solid rgba(47, 111, 115, .22); }
.record-node strong { overflow-wrap: anywhere; line-height: 1.2; }
.node-type, .record-meta, .record-detail small {
  color: var(--muted);
  font-size: 12px;
}
.record-node img {
  width: 100%;
  height: auto;
  max-height: 150px;
  border-radius: 6px;
  object-fit: contain;
  background: #fff;
}
.audio-play-wrap { display: inline-flex; }
.audio-play-wrap audio { display: none; }
.audio-play-button {
  min-height: 30px;
  padding: 3px 10px;
  font-size: 12px;
}

.mobile-nudge {
  display: none;
  grid-template-columns: auto repeat(4, 1fr);
  gap: 8px;
  align-items: center;
  padding: 10px;
  border-top: 1px solid var(--line);
}

.detail-panel { gap: 12px; }
.popup-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.popup-heading h2 { margin: 0; }
.popup-close {
  display: grid;
  width: 34px;
  min-width: 34px;
  height: 34px;
  min-height: 34px;
  place-items: center;
  padding: 0;
  font-size: 22px;
  line-height: 1;
}

.selected-record {
  display: grid;
  gap: 8px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}
.delete-record {
  justify-self: end;
  min-height: 30px;
  padding: 3px 8px;
  font-size: 12px;
}

.record-detail {
  display: grid;
  gap: 8px;
}
.record-detail h3 { margin: 0; overflow-wrap: anywhere; }
.record-detail p { margin: 0; color: var(--muted); overflow-wrap: anywhere; }
.record-detail img { width: 100%; max-height: 260px; object-fit: contain; border-radius: 8px; background: #fff; }
.record-detail audio { width: 100%; }
.record-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.solo-map {
  padding: 0;
  overflow: hidden;
}
.map-toolbar.simple button { min-width: 94px; }

.abstract-map {
  position: relative;
  height: min(68svh, 680px);
  min-height: 460px;
  overflow: hidden;
  background:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px),
    #e9e4d7;
  background-size: 56px 56px, 56px 56px;
}
.map-empty {
  position: absolute;
  z-index: 2;
  top: 50%;
  left: 50%;
  width: min(80%, 320px);
  transform: translate(-50%, -50%);
  color: var(--muted);
  text-align: center;
  pointer-events: none;
}
.map-marker {
  position: absolute;
  z-index: 3;
  display: grid;
  width: 38px;
  min-width: 38px;
  height: 38px;
  place-items: center;
  padding: 0;
  transform: translate(-50%, -50%);
  border: 2px solid var(--ink);
  border-radius: 999px;
  box-shadow: 0 8px 18px rgba(0,0,0,.18);
  font-weight: 700;
}
.map-marker.cluster-active { color: #fff; opacity: 1; }
.map-marker.dimmed { opacity: .3; }
.map-marker.selected { outline: 5px solid rgba(0,0,0,.18); }
.user-marker {
  position: absolute;
  z-index: 4;
  width: 20px;
  height: 20px;
  transform: translate(-50%, -50%);
  border: 3px solid #fff;
  border-radius: 999px;
  background: #111;
  box-shadow: 0 0 0 10px rgba(0,0,0,.12);
}

.bottom-sheet {
  max-width: 780px;
  margin: 0 auto;
}
.popup-backdrop {
  position: fixed;
  z-index: 29;
  inset: 0;
  width: 100%;
  height: 100%;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: rgba(20, 20, 20, .22);
}
.record-popup {
  position: fixed;
  z-index: 30;
  left: 50%;
  bottom: 20px;
  width: min(520px, calc(100% - 32px));
  max-height: min(72svh, 620px);
  overflow: auto;
  transform: translateX(-50%);
}
.cluster-companions {
  display: grid;
  gap: 8px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}
.cluster-companions h3 { margin: 0; }
.cluster-record-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.cluster-record {
  display: grid;
  min-width: 0;
  gap: 5px;
  align-content: start;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.cluster-record strong,
.cluster-record p { overflow-wrap: anywhere; }
.cluster-record p { margin: 0; color: var(--muted); font-size: 13px; }
.cluster-record img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: contain;
  background: var(--soft);
}
.compact-list button {
  min-height: 34px;
  text-align: left;
}

@media (max-width: 1050px) {
  .edit-layout {
    grid-template-columns: 1fr;
  }
  .side-panel {
    max-height: none;
  }
  .mobile-nudge {
    display: grid;
  }
}

@media (max-width: 640px) {
  .app-shell:has(.edit-page),
  .app-shell:has(.explore-page) {
    height: 100svh;
    min-height: 100svh;
    overflow: hidden;
  }
  main { padding: 10px; }
  main:has(.edit-page),
  main:has(.explore-page) {
    flex: 1;
    min-height: 0;
    overflow: hidden;
    padding: 0;
  }
  .app-header {
    grid-template-columns: 1fr;
    padding: 12px;
  }
  .app-header h1 { font-size: 20px; }
  .tabs { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .tab { min-width: 0; padding-inline: 4px; }
  .panel { padding: 12px; }
  .canvas-panel, .solo-map { padding: 0; }
  .main-input { min-height: 190px; }
  .manual-grid { grid-template-columns: 1fr; }
  .edit-page,
  .explore-page {
    position: relative;
    height: 100%;
    min-height: 0;
    gap: 0;
  }
  .edit-layout,
  .canvas-panel,
  .solo-map {
    height: 100%;
    min-height: 0;
  }
  .edit-layout { display: block; }
  .canvas-panel { border: 0; border-radius: 0; }
  .edit-canvas { height: calc(100% - 55px); min-height: 0; }
  .edge-layer { width: 2400px; height: 1600px; }
  .record-node { width: 164px; min-height: 92px; }
  .mobile-nudge { grid-template-columns: repeat(4, 1fr); }
  .mobile-nudge span { grid-column: 1 / -1; }
  .abstract-map { height: calc(100% - 55px); min-height: 0; }
  .detail-panel {
    display: none;
    position: absolute;
    z-index: 30;
    right: 10px;
    bottom: 10px;
    left: 10px;
    max-height: min(62svh, 520px);
    overflow: auto;
    box-shadow: 0 14px 36px rgba(0,0,0,.24);
  }
  .detail-panel.is-open { display: grid; }
  .record-popup {
    right: 10px;
    bottom: 10px;
    left: 10px;
    width: auto;
    max-height: 62svh;
    transform: none;
  }
  .explore-options {
    position: absolute;
    z-index: 8;
    right: 10px;
    bottom: 10px;
    width: auto;
    margin: 0;
    padding: 8px 10px;
    box-shadow: var(--shadow);
  }
  .explore-options .subtle-details {
    border: 0;
    padding: 0;
  }
}
