:root {
  --bg: #0f1115;
  --panel: #171a21;
  --line: #262a33;
  --text: #e8eaed;
  --muted: #8b919c;
  --accent: #4f8cff;
  --mine: #2b4f9a;
  --theirs: #22262f;
  --danger: #e5484d;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

button, input, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 10px;
  padding: .7rem 1rem;
  cursor: pointer;
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.danger { color: var(--danger); border-color: transparent; background: transparent; }

/* Startsida */
.screen { min-height: 100%; }
#home {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 1.5rem;
}
.card {
  width: 100%; max-width: 380px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1.75rem;
}
h1 { margin: 0 0 .25rem; font-size: 1.6rem; letter-spacing: -.01em; }
.muted { color: var(--muted); margin: 0 0 1.5rem; font-size: .95rem; }
#createBtn { width: 100%; padding: .9rem; }
.divider { display: flex; align-items: center; gap: .75rem; color: var(--muted); margin: 1.25rem 0; font-size: .85rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
#joinForm { display: flex; gap: .5rem; }
#codeInput {
  flex: 1; min-width: 0;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: .7rem .9rem; text-transform: uppercase; letter-spacing: .15em; font-family: ui-monospace, monospace;
}
#codeInput:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.notice { min-height: 1.2em; margin: .75rem 0 0; font-size: .9rem; }
.notice.error { color: var(--danger); }
.notice.ok { color: #46a758; }
.howto { margin: 0 0 1.5rem; padding-left: 1.2rem; color: var(--text); font-size: .92rem; }
.howto li { margin-bottom: .45rem; }
.howto li::marker { color: var(--accent); font-weight: 700; }
.mono { font-family: ui-monospace, monospace; letter-spacing: .05em; }
.rules { margin: 1.25rem 0 0; padding-top: 1rem; border-top: 1px solid var(--line); color: var(--muted); font-size: .8rem; }
.footnote { color: var(--muted); font-size: .8rem; margin-top: 1rem; }

/* Chatt */
#chat { display: flex; flex-direction: column; height: 100dvh; }
.bar {
  display: flex; align-items: center; gap: .5rem;
  padding: .6rem .75rem; padding-top: max(.6rem, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.share { padding: .45rem .8rem; }
.code { font-family: ui-monospace, monospace; letter-spacing: .1em; font-weight: 700; padding: .45rem .7rem; }
.status { flex: 1; color: var(--muted); font-size: .85rem; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.status.online { color: #46a758; }

.messages {
  flex: 1; overflow-y: auto; list-style: none; margin: 0;
  padding: 1rem .75rem; display: flex; flex-direction: column; gap: .4rem;
  overscroll-behavior: contain;
}
.msg {
  max-width: 80%; padding: .55rem .8rem; border-radius: 16px;
  background: var(--theirs); align-self: flex-start;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.mine { background: var(--mine); align-self: flex-end; }
.msg.image { padding: 4px; background: transparent; }
.msg.image img { display: block; max-width: 100%; max-height: 60vh; border-radius: 12px; }
.msg time { display: block; font-size: .7rem; color: rgba(255,255,255,.5); margin-top: .15rem; text-align: right; }
.msg.image time { padding: 0 .4rem .2rem; }
.system { align-self: center; color: var(--muted); font-size: .8rem; padding: .25rem 0; }

.typing { color: var(--muted); font-size: .8rem; padding: 0 1rem .25rem; }

.composer {
  display: flex; align-items: flex-end; gap: .5rem;
  padding: .6rem .75rem; padding-bottom: max(.6rem, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--panel);
}
.attach {
  display: grid; place-items: center; width: 44px; height: 44px; flex: none;
  border: 1px solid var(--line); border-radius: 10px; cursor: pointer; font-size: 1.3rem; color: var(--muted);
}
textarea {
  flex: 1; resize: none; max-height: 140px; min-height: 44px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: .6rem .8rem;
}
#sendBtn { height: 44px; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: #000c; border: 1px solid var(--line); padding: .5rem .9rem; border-radius: 10px; font-size: .9rem;
  max-width: 90vw; text-align: center;
}

/* Bilder: visa en gång */
.msg.photo { display: flex; align-items: center; gap: .5rem; cursor: default; user-select: none; }
.msg.photo .icon { font-size: 1.2rem; }
.msg.photo.openable { cursor: pointer; background: var(--accent); color: #fff; }
.msg.photo.done { opacity: .6; }

.timepicker {
  display: flex; align-items: center; gap: .4rem; flex-wrap: wrap;
  padding: .6rem .75rem; border-top: 1px solid var(--line); background: var(--panel); font-size: .9rem;
}
.timepicker span { color: var(--muted); margin-right: .25rem; }
.timepicker button { padding: .5rem .8rem; }
.timepicker .ghost { background: transparent; border-color: transparent; color: var(--muted); margin-left: auto; }

.viewer {
  position: fixed; inset: 0; z-index: 50; background: #000;
  display: flex; align-items: center; justify-content: center;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.viewer img {
  max-width: 100%; max-height: 100%; object-fit: contain;
  pointer-events: none; user-select: none; -webkit-user-drag: none;
  visibility: hidden;               /* svart tills man håller inne */
}
.viewer.holding img { visibility: visible; }
.viewer-hint {
  position: absolute; left: 1.5rem; right: 1.5rem; top: 50%; transform: translateY(-50%);
  text-align: center; color: #fffc; font-size: 1.05rem; pointer-events: none;
}
.viewer.holding .viewer-hint { display: none; }
/* Genomskinlig sköld över bilden: långtryck/högerklick träffar inte bilden */
.viewer-shield { position: absolute; inset: 0; touch-action: none; cursor: pointer; }
.viewer-count {
  position: absolute; top: max(1rem, env(safe-area-inset-top)); right: 1rem; z-index: 2;
  background: #000a; border: 1px solid #fff3; border-radius: 999px;
  min-width: 2.4rem; height: 2.4rem; display: grid; place-items: center; font-weight: 700;
}

/* Ingenting skrivs ut */
@media print { body { display: none !important; } }

/* Motparten har stängt – läs klart innan radering */
.closing {
  padding: .6rem .9rem; background: #3a2a10; color: #ffcf7a;
  border-bottom: 1px solid #5a4318; font-size: .9rem; text-align: center;
}
.composer.locked { opacity: .5; }
