:root {
  --ink: #14201c;
  --ink-soft: #3d4f47;
  --paper: #f3f7f2;
  --panel: rgba(255, 255, 255, 0.72);
  --line: rgba(20, 32, 28, 0.12);
  --accent: #0f7a5f;
  --accent-deep: #0a5543;
  --accent-soft: #d8efe6;
  --warn: #b42318;
  --ok: #157a45;
  --shadow: 0 24px 60px rgba(20, 48, 38, 0.14);
  --radius: 18px;
  --font-display: "Fraunces", Georgia, serif;
  --font-body: "DM Sans", "Segoe UI", sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  overflow-x: clip;
}

body {
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--paper);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

.atmosphere {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(ellipse 80% 55% at 12% 18%, rgba(15, 122, 95, 0.22), transparent 55%),
    radial-gradient(ellipse 70% 50% at 88% 8%, rgba(214, 164, 74, 0.18), transparent 50%),
    radial-gradient(ellipse 60% 45% at 70% 90%, rgba(54, 98, 120, 0.14), transparent 55%),
    linear-gradient(160deg, #eef5ef 0%, #e7efe8 42%, #dfe9e4 100%);
}

.atmosphere::after {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.35;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.55'/%3E%3C/svg%3E");
  pointer-events: none;
}

.shell {
  width: min(920px, calc(100% - 2rem));
  margin: 0 auto;
  min-height: 100dvh;
  min-height: 100svh;
  display: grid;
  align-content: center;
  padding: 2.5rem 0 3rem;
  padding-left: env(safe-area-inset-left, 0);
  padding-right: env(safe-area-inset-right, 0);
  padding-bottom: max(3rem, env(safe-area-inset-bottom, 0px));
}

[hidden] {
  display: none !important;
}

.view {
  animation: rise 480ms cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(14px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.shell:has(.gate:not([hidden])) {
  justify-items: center;
  align-content: center;
  padding-top: 0;
  padding-bottom: max(5.5rem, env(safe-area-inset-bottom, 0px));
}

.gate {
  width: min(100%, 28rem);
}

.headline {
  margin: 0 0 2.5rem;
  max-width: 12ch;
  font-family: var(--font-display);
  font-size: clamp(2.05rem, 6vw, 3rem);
  font-weight: 700;
  letter-spacing: -0.035em;
  line-height: 1.05;
  color: var(--ink);
}

.lede {
  margin: 0 0 2rem;
  max-width: 34ch;
  color: var(--ink-soft);
  font-size: 1.05rem;
}

.pin-form {
  display: grid;
  gap: 0.75rem;
  max-width: 420px;
  width: 100%;
}

.pin-label,
.nick-label {
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.pin-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.65rem;
}

.pin-input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.95rem 1rem;
  font-family: var(--font-display);
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: 0.28em;
  text-align: center;
  color: var(--ink);
  background: var(--panel);
  backdrop-filter: blur(10px);
  outline: none;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

.pin-input::placeholder {
  color: rgba(61, 79, 71, 0.32);
  font-weight: 500;
}

.pin-input:focus {
  border-color: rgba(15, 122, 95, 0.55);
  box-shadow: 0 0 0 4px rgba(15, 122, 95, 0.14);
}

.mode-option {
  display: flex;
  align-items: flex-start;
  gap: 0.55rem;
  margin: 0.15rem 0 0;
  color: var(--ink);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}

.mode-option input {
  margin-top: 0.2rem;
  width: 1rem;
  height: 1rem;
  accent-color: var(--accent);
}

.mode-option-desc {
  display: inline;
  font-weight: 400;
  color: var(--ink-soft);
  font-size: 0.88rem;
}

.sas-code {
  font-family: var(--font-display);
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--accent-deep);
  padding: 0.15rem 0.45rem;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.65);
  border: 1px solid var(--line);
}

.error {
  margin: 0;
  color: var(--warn);
  font-size: 0.92rem;
}

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 0.85rem 1.15rem;
  font-family: var(--font-body);
  font-size: 0.98rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 140ms ease, background 140ms ease, border-color 140ms ease;
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  background: var(--accent);
  color: #f7fffb;
}

.pin-form .btn-primary {
  margin-top: 0.35rem;
}

.btn-primary:hover {
  background: var(--accent-deep);
}

.btn-ghost {
  background: rgba(255, 255, 255, 0.55);
  border-color: var(--line);
  color: var(--ink);
}

.btn-ghost:hover {
  background: rgba(255, 255, 255, 0.9);
}

.btn-sm {
  padding: 0.45rem 0.75rem;
  font-size: 0.88rem;
}

.shell:has(.room:not([hidden])) {
  align-content: stretch;
  width: min(960px, calc(100% - 2rem));
  height: 100dvh;
  height: 100svh;
  min-height: 100dvh;
  min-height: 100svh;
  padding-top: max(0.75rem, env(safe-area-inset-top, 0px));
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom, 0px));
  overflow: hidden;
}

.room {
  display: grid;
  grid-template-rows: auto auto 1fr auto auto;
  gap: 0.65rem;
  height: 100%;
  min-height: 0;
  width: 100%;
  min-width: 0;
}

.room-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 0.55rem 0.75rem;
  align-items: center;
  grid-template-areas:
    "mode conn leave"
    "pin peers peers";
}

.room-meta,
.status-group {
  display: contents;
}

.mode-row {
  grid-area: mode;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem 0.65rem;
  min-width: 0;
}

.mode-badge {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-deep);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.mode-badge[data-mode="multi"] {
  background: rgba(54, 98, 120, 0.14);
  color: #2a4d5c;
}

.pin-chip {
  grid-area: pin;
  justify-self: start;
  display: inline-flex;
  align-items: center;
  flex-wrap: nowrap;
  gap: 0.45rem;
  max-width: 100%;
  min-width: 0;
  padding: 0.35rem 0.55rem 0.35rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.55);
}

.pin-chip-label {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
}

.pin-chip strong {
  font-family: var(--font-display);
  letter-spacing: 0.16em;
  font-size: 1.05rem;
  min-width: 7.2ch;
  text-align: center;
}

.chip-icon-btn {
  appearance: none;
  display: inline-grid;
  place-items: center;
  width: 1.7rem;
  height: 1.7rem;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--accent-deep);
  cursor: pointer;
  padding: 0;
  opacity: 0.8;
}

.chip-icon-btn:hover {
  opacity: 1;
  background: rgba(15, 122, 95, 0.1);
}

.chip-icon-btn.is-copied {
  color: var(--ok);
  opacity: 1;
}

.chip-icon {
  width: 0.95rem;
  height: 0.95rem;
  display: block;
}

.chip-icon-btn[aria-pressed="true"] .eye-open {
  display: none;
}

.chip-icon-btn[aria-pressed="false"] .eye-closed,
.chip-icon-btn:not([aria-pressed]) .eye-closed {
  display: none;
}

#conn-status {
  grid-area: conn;
  justify-self: end;
}

#leave-btn {
  grid-area: leave;
  justify-self: end;
}

.peers-wrap {
  grid-area: peers;
  justify-self: end;
  position: relative;
}

@media (min-width: 641px) {
  .room-bar {
    grid-template-columns: auto auto minmax(0, 1fr) auto auto;
    grid-template-areas: "mode pin conn peers leave";
    gap: 0.55rem 0.85rem;
  }

  #conn-status {
    justify-self: end;
  }

  .pin-chip {
    justify-self: start;
  }
}

.status,
.peers {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.88rem;
  color: var(--ink-soft);
}

button.peers {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  padding: 0.2rem 0.4rem;
  border-radius: 8px;
  font: inherit;
  cursor: pointer;
}

button.peers:hover,
button.peers[aria-expanded="true"] {
  border-color: var(--line);
  background: rgba(255, 255, 255, 0.65);
  color: var(--accent-deep);
}

.peer-list {
  position: absolute;
  top: calc(100% + 0.35rem);
  right: 0;
  z-index: 20;
  min-width: 10rem;
  max-width: 16rem;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(10px);
  box-shadow: var(--shadow);
}

.peer-list-title {
  margin: 0 0 0.35rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.peer-list-ul {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 0.3rem;
  max-height: 12rem;
  overflow: auto;
}

.peer-list-ul li {
  font-size: 0.9rem;
  color: var(--ink);
  padding: 0.2rem 0.15rem;
  word-break: break-word;
}

.peer-list-ul li.is-me {
  font-weight: 700;
  color: var(--accent-deep);
}

.peer-list-empty {
  color: var(--ink-soft);
  font-size: 0.85rem;
}

.status::before {
  content: "";
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: #9aa8a1;
}

.status[data-state="connected"]::before {
  background: var(--ok);
  box-shadow: 0 0 0 4px rgba(21, 122, 69, 0.15);
}

.status[data-state="connecting"]::before,
.status[data-state="reconnecting"]::before {
  background: #c48a1a;
  animation: pulse 1.2s ease-in-out infinite;
}

.status[data-state="offline"]::before {
  background: var(--warn);
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(0.85);
    opacity: 0.65;
  }
}

.nick-bar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.55rem;
  align-items: center;
}

.nick-input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.55rem 0.75rem;
  font: inherit;
  color: var(--ink);
  background: var(--panel);
  outline: none;
}

.nick-input:focus {
  border-color: rgba(15, 122, 95, 0.45);
  box-shadow: 0 0 0 3px rgba(15, 122, 95, 0.12);
}

.chat-log {
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow);
  min-height: 0;
}

.msg {
  display: grid;
  gap: 0.2rem;
  max-width: min(85%, 36rem);
  animation: rise 280ms cubic-bezier(0.22, 1, 0.36, 1) both;
}

.msg-mine {
  align-self: end;
  justify-items: end;
}

.msg-peer {
  align-self: start;
  justify-items: start;
}

.msg-meta {
  display: flex;
  gap: 0.45rem;
  align-items: baseline;
  font-size: 0.78rem;
  color: var(--ink-soft);
}

.msg-nick {
  font-weight: 700;
  color: var(--accent-deep);
}

.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 0.35rem;
}

.msg-mine .msg-row {
  flex-direction: row;
}

.msg-bubble {
  margin: 0;
  padding: 0.65rem 0.85rem;
  border-radius: 14px;
  font-size: 0.98rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-mine .msg-bubble {
  background: var(--accent);
  color: #f7fffb;
  border-bottom-right-radius: 5px;
}

.msg-peer .msg-bubble {
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--line);
  color: var(--ink);
  border-bottom-left-radius: 5px;
}

.msg-copy {
  appearance: none;
  flex: 0 0 auto;
  display: inline-grid;
  place-items: center;
  width: 1.7rem;
  height: 1.7rem;
  border: 0;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  padding: 0;
  border-radius: 8px;
  opacity: 0.72;
}

.msg-copy-icon {
  width: 0.95rem;
  height: 0.95rem;
  display: block;
}

.msg-copy:hover {
  opacity: 1;
  color: var(--accent-deep);
  background: rgba(15, 122, 95, 0.1);
}

.msg-copy.is-copied {
  opacity: 1;
  color: var(--ok);
}

.msg-system {
  align-self: center;
  max-width: 100%;
  text-align: center;
  font-size: 0.82rem;
  color: var(--ink-soft);
  padding: 0.15rem 0.5rem;
}

.composer {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.65rem;
  align-items: end;
  min-width: 0;
}

.file-input {
  display: none;
}

.attach-btn {
  width: 2.75rem;
  min-height: 3.2rem;
  padding: 0;
  display: inline-grid;
  place-items: center;
  align-self: stretch;
}

.attach-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.attach-icon {
  width: 1.15rem;
  height: 1.15rem;
  display: block;
}

.msg-media {
  display: grid;
  gap: 0.4rem;
  max-width: min(85%, 20rem);
}

.msg-image {
  display: block;
  max-width: 100%;
  max-height: 280px;
  border-radius: 12px;
  object-fit: contain;
  background: rgba(0, 0, 0, 0.04);
  cursor: zoom-in;
}

.msg-mine .msg-image {
  background: rgba(255, 255, 255, 0.12);
}

.msg-file-card {
  display: grid;
  gap: 0.35rem;
  padding: 0.65rem 0.8rem;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.9);
  min-width: 0;
  width: min(100%, 16rem);
}

.msg-mine .msg-file-card {
  background: rgba(255, 255, 255, 0.14);
  border-color: rgba(255, 255, 255, 0.2);
  color: #f7fffb;
}

.msg-file-name {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  word-break: break-word;
}

.msg-file-meta {
  margin: 0;
  font-size: 0.78rem;
  opacity: 0.8;
}

.msg-file-actions {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.msg-file-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.75);
  color: var(--accent-deep);
  border-radius: 8px;
  padding: 0.3rem 0.55rem;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

.msg-mine .msg-file-btn {
  border-color: rgba(255, 255, 255, 0.35);
  background: rgba(255, 255, 255, 0.16);
  color: #f7fffb;
}

.msg-progress {
  margin: 0;
  font-size: 0.78rem;
  color: var(--ink-soft);
}

.composer-input {
  width: 100%;
  min-width: 0;
  resize: none;
  min-height: 3.2rem;
  max-height: 9rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.75rem 0.9rem;
  font: inherit;
  line-height: 1.45;
  color: var(--ink);
  background: var(--panel);
  outline: none;
}

.composer-input:focus {
  border-color: rgba(15, 122, 95, 0.45);
  box-shadow: 0 0 0 3px rgba(15, 122, 95, 0.12);
}

.send-btn {
  min-width: 5.2rem;
  align-self: stretch;
}

.chat-hint {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.85rem;
}

@media (max-width: 640px) {
  .shell {
    width: min(100% - 1.25rem, 920px);
    align-content: start;
    padding-top: max(1.25rem, env(safe-area-inset-top, 0px));
    padding-bottom: max(1.5rem, env(safe-area-inset-bottom, 0px));
  }

  .shell:has(.gate:not([hidden])) {
    justify-items: center;
    padding-top: max(2.25rem, calc(env(safe-area-inset-top, 0px) + 1.5rem));
  }

  .shell:has(.room:not([hidden])) {
    width: 100%;
    max-width: none;
    height: 100dvh;
    height: 100svh;
    min-height: 100dvh;
    min-height: 100svh;
    padding: max(0.4rem, env(safe-area-inset-top, 0px))
      max(0.65rem, env(safe-area-inset-right, 0px))
      max(0.4rem, env(safe-area-inset-bottom, 0px))
      max(0.65rem, env(safe-area-inset-left, 0px));
    overflow: hidden;
  }

  .gate {
    width: 100%;
  }

  .headline {
    max-width: none;
    font-size: clamp(1.85rem, 9.5vw, 2.45rem);
    margin-bottom: 2.15rem;
  }

  .lede {
    max-width: none;
    margin-bottom: 1.5rem;
    font-size: 0.98rem;
  }

  .pin-form {
    max-width: none;
  }

  .pin-row {
    grid-template-columns: 1fr;
  }

  .pin-input {
    font-size: 1.35rem;
    letter-spacing: 0.18em;
    padding: 0.85rem 0.75rem;
  }

  .pin-form .btn-primary,
  .pin-form .btn-ghost {
    width: 100%;
  }

  .mode-option {
    font-size: 0.92rem;
  }

  .room {
    height: 100%;
    min-height: 0;
    gap: 0.4rem;
  }

  .room-bar {
    gap: 0.35rem 0.45rem;
  }

  .mode-row {
    gap: 0.25rem 0.35rem;
  }

  .mode-badge {
    font-size: 0.72rem;
  }

  .mode-row .sas-code {
    font-size: 0.7rem;
    padding: 0.1rem 0.35rem;
  }

  .pin-chip {
    width: auto;
    max-width: 100%;
    justify-self: start;
    padding: 0.2rem 0.4rem 0.2rem 0.5rem;
    gap: 0.25rem;
  }

  .pin-chip-label {
    font-size: 0.68rem;
    letter-spacing: 0.02em;
    flex-shrink: 0;
  }

  .pin-chip strong {
    font-size: 0.84rem;
    letter-spacing: 0.08em;
    min-width: 0;
    flex: 0 1 auto;
  }

  .pin-chip .chip-icon-btn {
    width: 1.4rem;
    height: 1.4rem;
    flex-shrink: 0;
  }

  .pin-chip .chip-icon {
    width: 0.85rem;
    height: 0.85rem;
  }

  .peers-wrap {
    justify-self: end;
    flex-shrink: 0;
  }

  button.peers {
    white-space: nowrap;
    padding: 0.15rem 0.25rem;
  }

  #conn-status {
    justify-self: center;
  }

  #leave-btn {
    padding: 0.35rem 0.55rem;
    font-size: 0.8rem;
  }

  .status,
  .peers {
    font-size: 0.8rem;
  }

  .peer-list {
    right: 0;
    left: auto;
    max-width: min(16rem, calc(100vw - 2rem));
  }

  .nick-bar {
    grid-template-columns: auto 1fr auto;
    gap: 0.4rem;
  }

  .nick-label {
    grid-column: auto;
    font-size: 0.75rem;
    letter-spacing: 0.02em;
    text-transform: none;
    white-space: nowrap;
  }

  .nick-input {
    padding: 0.4rem 0.55rem;
    border-radius: 10px;
    font-size: 0.92rem;
  }

  .chat-log {
    padding: 0.65rem 0.7rem;
    border-radius: 14px;
    gap: 0.55rem;
  }

  .msg,
  .msg-media {
    max-width: min(92%, 36rem);
  }

  .composer {
    gap: 0.4rem;
  }

  .attach-btn {
    width: 2.4rem;
    min-height: 2.55rem;
  }

  .composer-input {
    min-height: 2.55rem;
    max-height: 6.5rem;
    padding: 0.55rem 0.65rem;
    font-size: 1rem;
    border-radius: 12px;
  }

  .send-btn {
    min-width: 3.8rem;
    padding-left: 0.55rem;
    padding-right: 0.55rem;
  }

  .chat-hint {
    font-size: 0.72rem;
    line-height: 1.2;
  }
}

@media (max-width: 380px) {
  .composer {
    grid-template-columns: auto 1fr;
  }

  .send-btn {
    grid-column: 1 / -1;
    min-height: 2.6rem;
  }
}

@media (max-height: 520px) and (orientation: landscape) {
  .shell {
    align-content: start;
    padding-top: 1rem;
    padding-bottom: 1rem;
  }

  .shell:has(.room:not([hidden])) {
    height: 100dvh;
    height: 100svh;
    overflow: hidden;
  }

  .room {
    height: 100%;
    min-height: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .view,
  .status[data-state="connecting"]::before,
  .status[data-state="reconnecting"]::before {
    animation: none;
  }

  .btn {
    transition: none;
  }
}
