:root {
  --fog: #E8EBEE;
  --paper: #F8F9FA;
  --ink: #1F2328;
  --muted: #5B6470;
  --line: #CDD3D9;
  --mine: #D3DCE6;
  --tag: #F5C518;
  --tag-ink: #1F2328;
  --danger: #B42318;
  --focus: #2457C5;

  --font: "Instrument Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "Martian Mono", ui-monospace, Menlo, monospace;

  --r-bubble: 18px;
  --r-btn: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --fog: #16181B;
    --paper: #212429;
    --ink: #E6E8EB;
    --muted: #8D96A1;
    --line: #33383F;
    --mine: #2C3846;
    --danger: #F2685C;
    --focus: #7EA6FF;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --fog: #16181B;
  --paper: #212429;
  --ink: #E6E8EB;
  --muted: #8D96A1;
  --line: #33383F;
  --mine: #2C3846;
  --danger: #F2685C;
  --focus: #7EA6FF;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--fog);
  color: var(--ink);
  font: 400 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Buttons ---------- */
.btn {
  font: 600 15px/1 var(--font);
  border: 0;
  border-radius: var(--r-btn);
  padding: 13px 18px;
  cursor: pointer;
  color: inherit;
  background: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  white-space: nowrap;
}
.btn-ink { background: var(--ink); color: var(--fog); }
.btn-ink:hover { opacity: .88; }
.btn-ghost { box-shadow: inset 0 0 0 1.5px var(--line); }
.btn-ghost:hover { box-shadow: inset 0 0 0 1.5px var(--muted); }
.btn-danger { color: var(--danger); box-shadow: inset 0 0 0 1.5px color-mix(in srgb, var(--danger) 40%, transparent); }
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }
.btn-sm { padding: 9px 13px; font-size: 14px; }
.btn:disabled { opacity: .5; cursor: default; }

/* ---------- The tag: the one loud element ---------- */
.tag {
  position: relative;
  background: var(--tag);
  color: var(--tag-ink);
  /* luggage-tag silhouette: clipped corners on the left */
  clip-path: polygon(22px 0, 100% 0, 100% 100%, 22px 100%, 0 calc(100% - 22px), 0 22px);
  border-radius: 0 12px 12px 0;
}
.tag-hole {
  position: absolute;
  left: 20px;
  top: 50%;
  width: 14px;
  height: 14px;
  margin-top: -7px;
  border-radius: 50%;
  background: var(--fog);
  box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--tag-ink) 25%, transparent);
}

/* ---------- Home ---------- */
.home {
  min-height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding: 24px 16px;
  max-width: 760px;
  margin: 0 auto;
}
.brand { font-weight: 700; font-size: 18px; letter-spacing: -.01em; display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 30px; height: 30px; flex: none; }
.home-body { align-self: center; padding: 48px 0; }
.home h1 {
  font-size: clamp(36px, 7vw, 60px);
  line-height: 1.02;
  letter-spacing: -.035em;
  font-weight: 700;
  margin: 0 0 16px;
  max-width: 12ch;
}
.lede { color: var(--muted); font-size: 18px; max-width: 46ch; margin: 0 0 40px; }

.tag-input {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "label label" "field btn" "err err";
  align-items: center;
  gap: 6px 16px;
  padding: 22px 24px 22px 56px;
  max-width: 560px;
  transform: rotate(-1.2deg);
  transform-origin: left center;
}
.tag-label { grid-area: label; font-weight: 600; font-size: 14px; }
.code-field { grid-area: field; min-width: 0; }
.tag-input .btn { grid-area: btn; }
#code-input {
  width: 100%;
  min-width: 0;
  font: 700 clamp(30px, 7vw, 44px)/1.1 var(--mono);
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--tag-ink);
  background: transparent;
  border: 0;
  border-bottom: 2px dashed color-mix(in srgb, var(--tag-ink) 35%, transparent);
  padding: 4px 0 6px;
  border-radius: 0;
}
#code-input::placeholder { color: color-mix(in srgb, var(--tag-ink) 28%, transparent); }
#code-input:focus { outline: none; border-bottom-style: solid; border-bottom-color: var(--tag-ink); }
.tag-input .btn-ink { background: var(--tag-ink); color: var(--tag); }
.tag-error { grid-area: err; margin: 0; font-size: 14px; font-weight: 600; min-height: 0; }
.tag-error:empty { display: none; }

.or { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 16px; margin-top: 36px; color: var(--muted); }
.home-foot { color: var(--muted); font-size: 14px; max-width: 60ch; }

/* ---------- Room ---------- */
.room {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  max-width: 920px;
  margin: 0 auto;
}
.room-bar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.tag-chip {
  border: 0;
  cursor: pointer;
  padding: 10px 16px 10px 40px;
  display: inline-flex;
  align-items: baseline;
  gap: 12px;
  clip-path: polygon(14px 0, 100% 0, 100% 100%, 14px 100%, 0 calc(100% - 14px), 0 14px);
}
/* A freshly created session's tag drops onto its hook and settles. */
.tag-chip.swing { transform-origin: 19px 50%; animation: tag-swing 1.1s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes tag-swing {
  0% { transform: rotate(-14deg) translateY(-24px); opacity: 0; }
  35% { transform: rotate(7deg); opacity: 1; }
  60% { transform: rotate(-3.5deg); }
  80% { transform: rotate(1.5deg); }
  100% { transform: rotate(0); }
}

.room-mark { width: 34px; height: 34px; flex: none; }
.rys-ear-l { transform-origin: 8px 20px; }
.rys-ear-r { transform-origin: 24px 20px; }
.rys-eyes { transform-origin: 16px 23px; }
.room-mark.listen .rys-ear-l { animation: ear-l .5s ease 2; }
.room-mark.listen .rys-ear-r { animation: ear-r .5s ease .08s 2; }
.room-mark.listen .rys-eyes { animation: blink .35s ease .6s; }
@keyframes ear-l { 40% { transform: rotate(-10deg); } }
@keyframes ear-r { 40% { transform: rotate(10deg); } }
@keyframes blink { 50% { transform: scaleY(.1); } }

.tag-chip .tag-hole { left: 14px; width: 10px; height: 10px; margin-top: -5px; }
.code-text { font: 700 19px/1 var(--mono); letter-spacing: .06em; }
.chip-hint { font-size: 13px; font-weight: 600; opacity: .7; }
.room-meta { display: flex; flex-direction: column; min-width: 0; font-size: 14px; line-height: 1.35; }
.devices { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conn { color: var(--muted); }
.conn::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; background: var(--muted); vertical-align: 1px; }
.conn[data-state="open"]::before { background: #2E9E5B; }
.conn[data-state="closed"] { color: var(--danger); }
.conn[data-state="closed"]::before { background: var(--danger); }
.room-actions { margin-left: auto; display: flex; gap: 8px; }

.feed { overflow-y: auto; padding: 24px 16px; overscroll-behavior: contain; }
.empty { max-width: 44ch; margin: 12vh auto 0; text-align: center; color: var(--muted); }
.empty-title { font-size: 22px; font-weight: 700; color: var(--ink); margin: 0 0 8px; letter-spacing: -.02em; }
.empty strong { font-family: var(--mono); color: var(--ink); letter-spacing: .04em; }

.messages { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 14px; }
.msg { display: flex; flex-direction: column; align-items: flex-start; max-width: min(78%, 560px); }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg-meta { font-size: 12.5px; color: var(--muted); margin: 0 6px 4px; display: flex; gap: 8px; }
.msg-from { font-weight: 600; }
.msg.mine .msg-from { display: none; }
.bubble {
  background: var(--paper);
  border-radius: 6px var(--r-bubble) var(--r-bubble) var(--r-bubble);
  padding: 11px 15px;
  max-width: 100%;
  overflow-wrap: anywhere;
}
.msg.mine .bubble { background: var(--mine); border-radius: var(--r-bubble) 6px var(--r-bubble) var(--r-bubble); }
.bubble-text { white-space: pre-wrap; margin: 0; font: inherit; }
.bubble-text a { color: inherit; }
.msg.is-new .bubble { animation: arrive .28s cubic-bezier(.2, .8, .3, 1); }
@keyframes arrive { from { transform: translateY(8px) scale(.98); opacity: 0; } }

.bubble.has-image { padding: 4px; }
.bubble img { display: block; max-width: 100%; max-height: 360px; border-radius: calc(var(--r-bubble) - 4px); cursor: zoom-in; }
.file { display: flex; align-items: center; gap: 12px; text-decoration: none; color: inherit; min-width: 0; }
.file-icon {
  flex: none; width: 40px; height: 48px; border-radius: 4px 12px 4px 4px;
  background: var(--fog); display: grid; place-items: center;
  font: 700 10px/1 var(--mono); color: var(--muted); text-transform: uppercase;
}
.msg.mine .file-icon { background: color-mix(in srgb, var(--paper) 60%, var(--mine)); }
.file-name { font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 36ch; }
.file-size { font-size: 13px; color: var(--muted); }
.image-caption { padding: 6px 10px 4px; font-size: 13px; color: var(--muted); }

.progress { height: 4px; border-radius: 2px; background: var(--line); margin-top: 10px; overflow: hidden; width: 100%; min-width: 180px; }
.progress > span { display: block; height: 100%; width: 0; background: var(--ink); transition: width .15s linear; }
.msg.failed .bubble { box-shadow: inset 0 0 0 1.5px var(--danger); }

.msg-actions { display: flex; gap: 2px; margin: 4px 2px 0; }
.act {
  font: 600 13px/1 var(--font); color: var(--muted); background: none; border: 0;
  padding: 6px 8px; border-radius: 6px; cursor: pointer; text-decoration: none;
}
.act:hover { color: var(--ink); background: color-mix(in srgb, var(--ink) 7%, transparent); }

.composer {
  display: flex; align-items: flex-end; gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}
.attach { padding: 12px; flex: none; }
#text-input {
  flex: 1; min-width: 0; resize: none; max-height: 40vh;
  font: inherit; color: inherit; background: var(--paper);
  border: 1.5px solid var(--line); border-radius: 14px; padding: 11px 14px;
}
#text-input:focus { outline: none; border-color: var(--ink); }

.drop {
  position: fixed; inset: 0; z-index: 10;
  background: color-mix(in srgb, var(--fog) 80%, transparent);
  display: grid; place-items: center; padding: 24px;
  backdrop-filter: blur(4px);
}
.drop { overflow: hidden; }
/* The lynx rises from the bottom edge whenever files hover over the window. */
.drop-rys {
  position: absolute; left: 50%; bottom: 0; width: 140px; margin-left: -70px;
  transform: translateY(6%);
  animation: rys-peek .5s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes rys-peek { from { transform: translateY(100%); } }
.drop-inner {
  width: min(560px, 100%); padding: 64px 24px; text-align: center;
  border: 2.5px dashed var(--ink); border-radius: 24px;
  font-size: 24px; font-weight: 700; letter-spacing: -.02em;
}

.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); z-index: 20;
  background: var(--ink); color: var(--fog); padding: 10px 16px; border-radius: 10px;
  font-weight: 600; font-size: 14px; max-width: calc(100% - 32px);
}

.viewer {
  position: fixed; inset: 0; z-index: 30;
  background: #0E1013; color: #E6E8EB;
  display: grid; grid-template-rows: auto 1fr;
}
.viewer-bar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px calc(10px) 16px;
  padding-top: max(10px, env(safe-area-inset-top));
}
.viewer-name { flex: 1; min-width: 0; font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-tools { display: flex; align-items: center; gap: 4px; }
.vbtn {
  font: 600 16px/1 var(--font); color: inherit; background: rgba(255, 255, 255, .08);
  border: 0; border-radius: 8px; min-width: 38px; height: 38px; padding: 0 10px;
  display: inline-grid; place-items: center; cursor: pointer; text-decoration: none;
}
.vbtn:hover { background: rgba(255, 255, 255, .16); }
.vbtn-text { font-size: 14px; }
.viewer-zoom { font: 500 13px/1 var(--mono); min-width: 64px; }
.viewer-stage {
  position: relative; overflow: hidden; touch-action: none; user-select: none;
  display: grid; place-items: center; min-height: 0; cursor: zoom-in;
}
.viewer-stage.can-pan { cursor: grab; }
.viewer-stage.can-pan:active { cursor: grabbing; }
/* Rendered at natural size; JS scales it down to fit. */
.viewer-stage img {
  position: absolute; left: 50%; top: 50%;
  max-width: none; max-height: none;
  translate: -50% -50%;
  transform-origin: center;
  border-radius: 0; cursor: inherit;
}

/* ---------- QR, dialogs, device name ---------- */
.qr svg { display: block; width: 100%; height: auto; }
.qr { background: #fff; border-radius: 12px; padding: 10px; }
.empty .qr { width: 148px; margin: 0 auto 20px; }
.qr-big { width: min(280px, 70vw); margin: 0 auto; }

.sheet {
  border: 0; border-radius: 20px; padding: 24px;
  background: var(--paper); color: var(--ink);
  width: min(380px, calc(100% - 32px)); text-align: center;
}
.sheet::backdrop { background: rgba(10, 12, 14, .55); }
.sheet-code { font: 700 26px/1 var(--mono); letter-spacing: .06em; margin: 20px 0 6px; }
.sheet-link { color: var(--muted); font-size: 14px; margin: 0 0 20px; overflow-wrap: anywhere; }
.sheet .btn { width: 100%; }
#name-form { text-align: left; }
.sheet-title { display: block; font-weight: 700; font-size: 18px; letter-spacing: -.01em; }
.sheet-hint { color: var(--muted); font-size: 14px; margin: 4px 0 14px; }
#name-input {
  width: 100%; font: inherit; color: inherit; background: var(--fog);
  border: 1.5px solid var(--line); border-radius: 10px; padding: 11px 13px;
}
#name-input:focus { outline: none; border-color: var(--ink); }
.sheet-actions { display: flex; gap: 8px; margin-top: 16px; }

.me-name {
  font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer;
  text-decoration: underline dotted; text-underline-offset: 3px;
}
.me-name:hover { text-decoration-style: solid; }

/* ---------- "Więcej" menu ---------- */
.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 15;
  width: min(290px, calc(100vw - 32px)); padding: 6px;
  background: var(--paper); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .18);
}
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 4px; }
.menu-item {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px; width: 100%;
  font: 600 14px/1.3 var(--font); color: var(--ink); text-align: left;
  background: none; border: 0; border-radius: 8px; padding: 9px 10px; cursor: pointer;
}
.menu-item small { font-weight: 400; font-size: 12.5px; color: var(--muted); }
.menu-item:hover:not(:disabled), .menu-item:focus-visible { background: color-mix(in srgb, var(--ink) 7%, transparent); }
.menu-item:disabled { opacity: .45; cursor: default; }
.menu-danger, .menu-danger small { color: var(--danger); }
.menu-item[aria-pressed] > span::before {
  content: ""; display: inline-block; width: 26px; height: 15px; margin-right: 8px; vertical-align: -2px;
  border-radius: 8px;
  background: radial-gradient(circle at 7.5px 50%, var(--paper) 5px, transparent 5.5px), var(--line);
}
.menu-item[aria-pressed="true"] > span::before {
  background: radial-gradient(circle at 18.5px 50%, var(--paper) 5px, transparent 5.5px), var(--ink);
}

.status-line { display: flex; gap: 10px; align-items: baseline; }
.lock-state { font-weight: 600; font-size: 13px; }
.lock-state::before {
  content: ""; display: inline-block; width: 9px; height: 7px; margin-right: 5px; vertical-align: 0;
  border: 2px solid currentColor; border-radius: 2px; box-shadow: 0 -3px 0 -1px currentColor;
}

/* ---------- Inline media ---------- */
.bubble.has-media { padding: 4px; }
.bubble video { display: block; max-width: 100%; max-height: 360px; border-radius: calc(var(--r-bubble) - 4px); background: #000; }
.bubble audio { display: block; width: min(320px, 70vw); margin: 6px 6px 2px; }

@media (max-width: 640px) {
  .menu-wrap { flex: 1; }
  .menu-wrap > .btn { width: 100%; }
  .room-bar { flex-wrap: wrap; gap: 10px 12px; }
  .room-actions { margin-left: 0; width: 100%; }
  .room-actions .btn { flex: 1; }
  .msg { max-width: 90%; }
  .file-name { max-width: 22ch; }
  .viewer-name { display: none; }
  .viewer-tools { width: 100%; justify-content: space-between; }
  .tag-input { grid-template-columns: 1fr; grid-template-areas: "label" "field" "btn" "err"; transform: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
