/* ezchat: black, white, tomato. Mobile-first, small-group chat.
   Token system: two neutrals (black/white) plus exactly one accent
   (tomato) used boldly on your own messages, so the accent reads as
   "yours" everywhere it appears rather than decoration. No external
   fonts/CDNs, system stacks only, so nothing here ever makes a
   third-party network request. */

:root {
  --bg: #ffffff;
  --bg-elevated: #fafafa;
  --ink: #0a0a0a;
  --ink-soft: #6b6b6b;
  --line: #e2e2e2;

  --tomato: #ff6347;
  --tomato-dark: #e5533c;
  --tomato-soft: #ffe4de;
  --on-tomato: #ffffff;

  --bubble-theirs: #fafafa;

  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 9px;

  --shadow: 0 2px 16px rgba(0, 0, 0, 0.1);

  --font-display: "Iowan Old Style", "Palatino Linotype", Georgia, "Times New Roman", serif;
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a0a0a;
    --bg-elevated: #161616;
    --ink: #f5f5f5;
    --ink-soft: #a3a3a3;
    --line: #2a2a2a;

    --tomato: #ff7a61;
    --tomato-dark: #ff6347;
    --tomato-soft: #3a1f19;
    --on-tomato: #0a0a0a;

    --bubble-theirs: #161616;

    --shadow: 0 2px 20px rgba(0, 0, 0, 0.5);
  }
}

/* The panel's dark-mode toggle stamps one of these on <html>, taking
   precedence over the OS-level media query above in either direction:
   picking "light" while the OS is in dark mode, or "dark" while it isn't. */
:root[data-theme="dark"] {
  --bg: #0a0a0a;
  --bg-elevated: #161616;
  --ink: #f5f5f5;
  --ink-soft: #a3a3a3;
  --line: #2a2a2a;

  --tomato: #ff7a61;
  --tomato-dark: #ff6347;
  --tomato-soft: #3a1f19;
  --on-tomato: #0a0a0a;

  --bubble-theirs: #161616;

  --shadow: 0 2px 20px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #ffffff;
  --bg-elevated: #fafafa;
  --ink: #0a0a0a;
  --ink-soft: #6b6b6b;
  --line: #e2e2e2;

  --tomato: #ff6347;
  --tomato-dark: #e5533c;
  --tomato-soft: #ffe4de;
  --on-tomato: #ffffff;

  --bubble-theirs: #fafafa;

  --shadow: 0 2px 16px rgba(0, 0, 0, 0.1);
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  /* iOS zooms on double-tap by default; this plus touch-action below keeps
     the layout at a fixed scale no matter how someone taps. */
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
}

#app {
  height: 100dvh;
  height: 100vh; /* fallback for browsers without dvh support */
  display: flex;
  flex-direction: column;
}

a {
  color: var(--tomato-dark);
}

:focus-visible {
  outline: 2px solid var(--tomato);
  outline-offset: 2px;
}

button {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
}

[hidden] {
  display: none !important;
}

/* ---------------- top bar ---------------- */

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(12px, env(safe-area-inset-top)) 14px 12px;
  gap: 10px;
  border-bottom: 1px solid var(--line);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.btn-panel-toggle {
  flex: none;
  width: 38px;
  height: 38px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--bg-elevated);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.btn-panel-toggle svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--ink);
  font-family: var(--font-display);
  font-size: 1.2rem;
  font-weight: 600;
  white-space: nowrap;
}

.brand-mark {
  width: 24px;
  height: 24px;
  fill: var(--ink);
}

.brand-spark {
  fill: var(--tomato);
}

.room-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--ink-soft);
  white-space: nowrap;
}

/* ---------------- layout: sidebar + main ---------------- */

.layout {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

#panel-backdrop {
  display: none;
}

#users-panel {
  flex: none;
  width: 240px;
  background: var(--bg-elevated);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.18s ease;
}

#users-panel.panel-closed {
  width: 0;
  border-right: none;
}

.users-panel-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px 10px;
}

.users-panel-title {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
  margin: 4px 8px 10px;
  white-space: nowrap;
}

.panel-footer {
  flex: none;
  border-top: 1px solid var(--line);
  padding: 8px;
}

.theme-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.theme-toggle:hover {
  background: var(--tomato-soft);
  color: var(--tomato-dark);
}

.theme-toggle svg {
  flex: none;
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.user-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-radius: var(--radius-sm);
  white-space: nowrap;
}

.user-row.me {
  background: var(--tomato-soft);
}

.user-name {
  font-weight: 600;
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-name .you-tag {
  color: var(--ink-soft);
  font-weight: 400;
}

@media (max-width: 899px) {
  #users-panel {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: 78vw;
    max-width: 320px;
    z-index: 20;
    transform: translateX(0);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }

  .users-panel-scroll {
    padding-top: max(14px, env(safe-area-inset-top));
  }

  .panel-footer {
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  #users-panel.panel-closed {
    transform: translateX(-100%);
    width: 78vw;
    border-right: 1px solid var(--line);
  }

  #panel-backdrop.visible {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 15;
  }
}

@media (prefers-reduced-motion: reduce) {
  #users-panel {
    transition: none;
  }
}

/* ---------------- avatars ---------------- */

.avatar-wrap {
  position: relative;
  flex: none;
  display: inline-flex;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  overflow: hidden;
  background: var(--bg);
  border: 1px solid var(--line);
  flex: none;
}

.avatar svg {
  display: block;
  width: 100%;
  height: 100%;
}

.avatar svg rect {
  fill: var(--tomato);
}

.mood-badge {
  position: absolute;
  bottom: -3px;
  right: -3px;
  font-size: 12px;
  line-height: 1;
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  border-radius: 50%;
  padding: 1px;
}

/* ---------------- screens (create / share / join) ---------------- */

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#screen-create,
#screen-share,
#screen-join {
  align-items: center;
  justify-content: center;
  padding: 24px 18px 48px;
  overflow-y: auto;
}

.card {
  width: 100%;
  max-width: 440px;
}

.seal-card {
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 28px 26px 30px;
}

.eyebrow {
  margin: 0 0 6px;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--tomato-dark);
  font-weight: 600;
}

h1 {
  font-family: var(--font-display);
  font-size: 1.6rem;
  line-height: 1.25;
  margin: 0 0 10px;
}

.lede {
  color: var(--ink-soft);
  font-size: 0.95rem;
  margin: 0 0 22px;
}

/* ---------------- form controls ---------------- */

.field {
  display: block;
  margin-bottom: 18px;
}

.field > span {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.field-hint {
  display: block;
  font-size: 0.76rem;
  font-weight: 400;
  color: var(--ink-soft);
  margin-top: 5px;
}

input[type="text"],
input[type="number"],
select {
  width: 100%;
  font: inherit;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
}

input[type="text"]:focus,
input[type="number"]:focus,
select:focus {
  border-color: var(--tomato);
}

.name-mood-row {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.name-mood-row input[type="text"] {
  flex: 1;
}

.mood-picker-wrap {
  position: relative;
  flex: none;
}

.btn-mood {
  width: 46px;
  height: 46px;
  border-radius: var(--radius-md);
  border: 1px solid var(--line);
  background: var(--bg);
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mood-grid {
  position: absolute;
  z-index: 5;
  top: calc(100% + 6px);
  right: 0;
  width: 220px;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 8px;
  box-shadow: var(--shadow);
}

.mood-grid button {
  border: none;
  background: transparent;
  font-size: 1.15rem;
  padding: 6px 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.mood-grid button:hover {
  background: var(--tomato-soft);
}

.chip-group {
  display: flex;
  gap: 10px;
}

.chip {
  flex: 1;
  min-height: 44px;
  border-radius: var(--radius-md);
  border: 1px solid var(--line);
  background: var(--bg);
  font-weight: 600;
  cursor: pointer;
}

.chip[aria-pressed="true"] {
  background: var(--tomato);
  border-color: var(--tomato-dark);
  color: var(--on-tomato);
}

.form-error {
  color: var(--tomato-dark);
  font-size: 0.85rem;
  margin: 12px 0 0;
}

/* ---------------- buttons ---------------- */

.btn-primary,
.btn-secondary,
.btn-ghost {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  padding: 0 20px;
  border-radius: var(--radius-md);
  border: none;
  cursor: pointer;
  font-weight: 600;
  text-decoration: none;
}

.btn-primary {
  width: 100%;
  background: var(--tomato);
  color: var(--on-tomato);
}

.btn-primary:hover {
  background: var(--tomato-dark);
}

.btn-secondary {
  background: var(--tomato-soft);
  color: var(--tomato-dark);
}

.btn-ghost {
  background: transparent;
  color: var(--ink-soft);
  border: 1px dashed var(--line);
  width: 100%;
  margin: 14px 0;
}

.link-row {
  display: flex;
  gap: 10px;
  margin-bottom: 14px;
}

.link-row input {
  flex: 1;
  font-family: var(--font-mono);
  font-size: 0.85rem;
}

.qr-wrap {
  display: flex;
  justify-content: center;
  padding: 18px;
  margin-bottom: 6px;
  background: #ffffff;
  border-radius: var(--radius-md);
}

.qr-wrap svg {
  width: 200px;
  height: 200px;
}

/* ---------------- chat screen ---------------- */

#screen-chat {
  min-height: 0;
  min-width: 0;
}

.chat-log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  list-style: none;
  margin: 0;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  max-width: 78%;
  transition: opacity 0.25s ease, transform 0.25s ease;
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.msg--gone {
  opacity: 0;
  transform: scale(0.85) translateY(4px);
}

@media (prefers-reduced-motion: reduce) {
  .msg {
    transition: none;
  }
}

.msg.system {
  align-self: center;
  max-width: 90%;
  color: var(--ink-soft);
  font-size: 0.82rem;
  text-align: center;
  display: block;
}

.msg.theirs {
  align-self: flex-start;
}

.msg.mine {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg-body {
  min-width: 0;
}

.msg-name {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--ink-soft);
  margin: 0 0 3px 2px;
}

.bubble {
  position: relative;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.msg.theirs .bubble {
  background: var(--bubble-theirs);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}

.msg.mine .bubble {
  background: var(--tomato);
  color: var(--on-tomato);
  border-bottom-right-radius: 4px;
}

.bubble-media {
  max-width: 240px;
  max-height: 240px;
  border-radius: var(--radius-sm);
  display: block;
}

.bubble-reveal {
  cursor: pointer;
  font-style: italic;
  color: var(--ink-soft);
}

.msg.mine .bubble-reveal {
  color: var(--on-tomato);
  opacity: 0.85;
}

/* Signature element: a small ember that cools from bright to ash as a
   self-destructing message counts down, rather than a plain numeral. Set
   via the CSSOM (element.style.setProperty), never an inline style
   attribute, so this works under a strict style-src 'self' CSP. */
.ember-dot {
  --progress: 1;
  position: absolute;
  bottom: -5px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: conic-gradient(var(--tomato) calc(var(--progress) * 360deg), var(--line) 0deg);
  border: 2px solid var(--bg);
}

.msg.mine .ember-dot {
  right: -3px;
  border-color: var(--tomato);
}

.msg.theirs .ember-dot {
  left: -3px;
}

@media (prefers-reduced-motion: no-preference) {
  .ember-dot.low {
    animation: ember-pulse 1s ease-in-out infinite;
  }
}

@keyframes ember-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 rgba(255, 99, 71, 0.5);
  }
  50% {
    box-shadow: 0 0 6px 1px rgba(255, 99, 71, 0.55);
  }
}

.composer {
  flex: none;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 12px max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg-elevated);
}

.composer input[type="text"] {
  flex: 1;
  border-radius: 999px;
  min-width: 0;
}

.burn-select {
  flex: none;
  width: auto;
  max-width: 92px;
  padding: 8px 6px;
  font-size: 0.78rem;
  border-radius: 999px;
}

.btn-icon,
.btn-send {
  flex: none;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--tomato-soft);
  color: var(--tomato-dark);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.btn-icon.recording {
  background: var(--tomato);
  color: var(--on-tomato);
}

.btn-icon svg,
.btn-send svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

.btn-send {
  background: var(--tomato);
  color: var(--on-tomato);
}

.btn-send:hover {
  background: var(--tomato-dark);
}

/* ---------------- toast ---------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.85rem;
  box-shadow: var(--shadow);
  z-index: 30;
  max-width: 90vw;
  text-align: center;
}
