*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --blue: #007AFF;
  --orange: #FF9500;
  --bg: #f2f2f7;
  --surface: #ffffff;
  --text: #1c1c1e;
  --text-secondary: #8e8e93;
  --border: rgba(60,60,67,.18);
  --tab-height: calc(56px + env(safe-area-inset-bottom));
  --safe-top: env(safe-area-inset-top);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000;
    --surface: #1c1c1e;
    --text: #fff;
    --text-secondary: #8e8e93;
    --border: rgba(255,255,255,.15);
  }
}

html { height: -webkit-fill-available; }

body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", sans-serif;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

/* ── App shell ───────────────────────────────────── */

#app {
  display: flex;
  flex-direction: column;
  height: 100%; /* Overskrives af JS med window.innerHeight */
}

.tab {
  display: none;
  flex: 1;
  min-height: 0;   /* kritisk: tillader flex-barn at krympe */
  overflow: hidden;
  position: relative;
}
.tab.active { display: flex; flex-direction: column; }

/* ── Kort ────────────────────────────────────────── */

#map {
  position: absolute;
  inset: 0;   /* fylder præcis det areal flex-layoutet har beregnet til tab'en */
}

.map-badge {
  position: absolute;
  top: calc(var(--safe-top) + 12px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--orange);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  padding: 5px 14px;
  border-radius: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,.25);
  pointer-events: none;
  z-index: 500;
}
.map-badge.hidden { display: none; }

/* Leaflet overrides */
.leaflet-control-attribution { font-size: 10px !important; }
.leaflet-container { font-family: inherit; }
.leaflet-control-zoom { margin-bottom: 12px !important; margin-right: 12px !important; }

/* Foto-pin */
.photo-pin {
  width: 52px; height: 52px;
  border-radius: 10px;
  border: 3px solid #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.3);
  object-fit: cover;
  display: block;
  background: #ddd;
}

/* Klynge-pin */
.cluster-pin { position: relative; width: 52px; height: 52px; }
.cluster-pin img {
  width: 52px; height: 52px;
  border-radius: 10px;
  border: 3px solid #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.3);
  object-fit: cover;
  display: block;
  filter: brightness(.7);
}
.cluster-badge {
  position: absolute; top: -6px; right: -6px;
  background: #FF3B30; color: #fff;
  font-size: 11px; font-weight: 700;
  min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 1px 3px rgba(0,0,0,.3);
}

.track-dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  border: 2.5px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.35);
}

/* ── Galleri ─────────────────────────────────────── */

#gallery-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  overscroll-behavior: contain;
  padding-top: var(--safe-top);
}

.date-header {
  padding: 14px 16px 6px;
  font-size: 15px; font-weight: 700;
  letter-spacing: -.02em;
  color: var(--text);
  position: sticky; top: 0;
  background: rgba(242,242,247,.88);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 10;
}
@media (prefers-color-scheme: dark) {
  .date-header { background: rgba(0,0,0,.82); }
}

.photo-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px; margin-bottom: 2px;
}

.thumb-cell {
  aspect-ratio: 1;
  overflow: hidden;
  background: var(--surface);
  cursor: pointer;
  position: relative;
}
.thumb-cell img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-cell .thumb-spinner {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
}
.thumb-cell .thumb-spinner::after {
  content: '';
  width: 24px; height: 24px;
  border: 2px solid var(--border);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

.empty-msg {
  text-align: center; color: var(--text-secondary);
  font-size: 15px; padding: 80px 32px;
}
.empty-msg.hidden { display: none; }

/* ── Billedvisning ───────────────────────────────── */

#viewer {
  position: fixed; inset: 0;
  background: #000;
  z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  touch-action: none;
}
#viewer.hidden { display: none; }

#viewer-stage {
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
#viewer-img {
  max-width: 100%; max-height: 100%;
  object-fit: contain;
  transform-origin: center;
  user-select: none; -webkit-user-select: none;
  will-change: transform;
}

#viewer-close {
  position: absolute;
  top: calc(var(--safe-top) + 12px); left: 16px;
  width: 36px; height: 36px; border-radius: 50%;
  background: rgba(255,255,255,.15); border: none; color: #fff;
  font-size: 17px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  z-index: 10; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
#viewer-counter {
  position: absolute;
  top: calc(var(--safe-top) + 18px); left: 50%; transform: translateX(-50%);
  color: rgba(255,255,255,.8); font-size: 14px; font-weight: 500;
  z-index: 10; pointer-events: none;
}
#viewer-prev, #viewer-next {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 44px; height: 44px; border-radius: 50%;
  background: rgba(255,255,255,.15); border: none; color: #fff;
  font-size: 28px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  z-index: 10; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
#viewer-prev { left: 12px; }
#viewer-next { right: 12px; }
#viewer-loader {
  position: absolute; inset: 0;
  display: none; align-items: center; justify-content: center; pointer-events: none;
}
#viewer-loader.active { display: flex; }
#viewer-loader::after {
  content: ''; width: 36px; height: 36px;
  border: 3px solid rgba(255,255,255,.2); border-top-color: #fff;
  border-radius: 50%; animation: spin .7s linear infinite;
}

/* ── Beskeder ────────────────────────────────────── */

#msg-setup {
  flex: 1; display: flex; align-items: center; justify-content: center;
  padding: 32px 24px; background: var(--bg);
}
.setup-card {
  background: var(--surface);
  border-radius: 20px; padding: 40px 28px;
  width: 100%; max-width: 360px; text-align: center;
  box-shadow: 0 4px 24px rgba(0,0,0,.08);
}
.setup-icon { font-size: 2.5rem; margin-bottom: 16px; }
.setup-card h2 { font-size: 1.3rem; font-weight: 700; margin-bottom: 8px; }
.setup-card p  { font-size: .9rem; color: var(--text-secondary); margin-bottom: 28px; }
.setup-card input {
  width: 100%; padding: 14px 16px;
  border: 1.5px solid var(--border); border-radius: 12px;
  font-size: 1rem; background: var(--bg); color: var(--text);
  outline: none; margin-bottom: 14px;
}
.setup-card input:focus { border-color: var(--blue); }
.setup-card button {
  width: 100%; padding: 15px; background: var(--blue);
  color: #fff; border: none; border-radius: 12px;
  font-size: 1rem; font-weight: 600; cursor: pointer;
}

#msg-chat {
  flex: 1; display: flex; flex-direction: column; min-height: 0;
}
#msg-chat.hidden { display: none; }

#msg-list {
  flex: 1; min-height: 0;
  overflow-y: scroll; -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  overscroll-behavior: contain;
  padding: calc(var(--safe-top) + 8px) 12px 12px;
  display: flex; flex-direction: column; gap: 8px;
}

.msg-bubble {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: .92rem; line-height: 1.45;
  word-break: break-word;
}
.msg-bubble.mine {
  align-self: flex-end;
  background: var(--blue); color: #fff;
  border-bottom-right-radius: 4px;
}
.msg-bubble.theirs {
  align-self: flex-start;
  background: var(--surface); color: var(--text);
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
}
.msg-meta {
  font-size: .72rem; opacity: .65; margin-top: 3px;
}
.msg-bubble.mine  .msg-meta { text-align: right; }
.msg-bubble.theirs .msg-meta { text-align: left; }

#msg-bar {
  display: flex; gap: 8px; align-items: center;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: .5px solid var(--border);
}
#msg-input {
  flex: 1; padding: 10px 14px;
  border: 1.5px solid var(--border); border-radius: 22px;
  font-size: .95rem; background: var(--bg); color: var(--text);
  outline: none;
}
#msg-input:focus { border-color: var(--blue); }
#msg-send {
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--blue); border: none; color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0;
}
#msg-send svg { width: 18px; height: 18px; }

/* ── Fanebjælke ──────────────────────────────────── */

#tab-bar {
  display: flex;
  height: var(--tab-height);
  padding-bottom: env(safe-area-inset-bottom);
  background: rgba(255,255,255,.9);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border-top: .5px solid var(--border);
  flex-shrink: 0;
  position: relative; z-index: 100;
}
@media (prefers-color-scheme: dark) {
  #tab-bar { background: rgba(28,28,30,.9); }
}

.tab-btn {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 3px;
  background: none; border: none;
  color: var(--text-secondary);
  font-size: 10px; font-weight: 500;
  cursor: pointer; padding: 8px 0 0;
  -webkit-tap-highlight-color: transparent;
  transition: color .15s;
}
.tab-btn svg { width: 24px; height: 24px; }
.tab-btn.active { color: var(--blue); }

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Responsive ──────────────────────────────────── */

/* Tæt-på-kanten billeder: giv piletasterne plads uden at dække billedet */
@media (max-width: 480px) {
  #viewer-prev, #viewer-next {
    width: 36px; height: 36px; font-size: 22px;
    background: rgba(0,0,0,.35);
  }
  #viewer-prev { left: 4px; }
  #viewer-next { right: 4px; }

  .setup-card { padding: 32px 20px; }
  .setup-card h2 { font-size: 1.1rem; }

  #msg-bar { padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); gap: 6px; }
}

/* Bred skærm (tablet/desktop): bredere galleri og centreret viewer */
@media (min-width: 768px) {
  .photo-row { grid-template-columns: repeat(4, 1fr); }

  #viewer-img { max-height: 90vh; }

  /* Piletasterne længere inde fra kanten på desktop */
  #viewer-prev { left: 24px; }
  #viewer-next { right: 24px; }

  .setup-card { padding: 48px 40px; }
}

@media (min-width: 1024px) {
  .photo-row { grid-template-columns: repeat(5, 1fr); }

  /* Beskeder max bredde på desktop */
  #msg-list { max-width: 680px; margin: 0 auto; }
  #msg-bar  { max-width: 680px; margin: 0 auto; }
}

/* Skjul pil-knapper på touch-enheder — brugerne swiper i stedet */
@media (hover: none) and (pointer: coarse) {
  #viewer-prev, #viewer-next { display: none; }
}
