:root {
  --ink: #12241f;
  --muted: #5a6f68;
  --paper: #f3f7f5;
  --panel: rgba(255, 255, 255, 0.82);
  --line: rgba(18, 36, 31, 0.12);
  --teal: #0b3d3a;
  --teal-2: #14635c;
  --teal-deep: #062826;
  --copper: #c46b3a;
  --copper-soft: #f3e0d4;
  --ok: #1f7a4d;
  --warn: #9a6b00;
  --err: #b42318;
  --shadow: 0 18px 50px rgba(11, 61, 58, 0.12);
  --radius: 18px;
  --font: "Sora", system-ui, sans-serif;
  --display: "Fraunces", Georgia, serif;
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}
body {
  margin: 0;
  color: var(--ink);
  font: 15px/1.45 var(--font);
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(196, 107, 58, 0.18), transparent 55%),
    radial-gradient(900px 500px at 100% 0%, rgba(20, 99, 92, 0.22), transparent 50%),
    linear-gradient(165deg, #dce8e4 0%, var(--paper) 42%, #e7efeb 100%);
  background-attachment: fixed;
}

.shell {
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  max-width: 1180px;
  margin: 0 auto;
  padding: 0.75rem clamp(0.75rem, 2vw, 1.25rem);
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  overflow: hidden;
  min-height: 0;
}

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.brand { display: flex; align-items: center; gap: 0.75rem; min-width: 0; }
.brand-mark {
  width: 42px; height: 42px; border-radius: 14px; flex: 0 0 auto;
  background:
    linear-gradient(145deg, var(--teal-2), var(--teal)),
    var(--teal);
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.25),
    0 3px 0 var(--teal-deep),
    0 8px 16px rgba(11, 61, 58, 0.2);
  position: relative;
}
.brand-mark::after {
  content: "";
  position: absolute; inset: 12px 10px 10px 10px;
  border-radius: 8px; background: rgba(232, 242, 239, 0.92);
}
.brand-name {
  margin: 0; font-family: var(--display); font-size: 1.35rem; font-weight: 700;
  letter-spacing: -0.02em; color: var(--teal);
}
.brand-sub { margin: 0; color: var(--muted); font-size: 0.78rem; }
.topbar-meta { display: flex; gap: 0.45rem; align-items: center; flex: 0 0 auto; }

.panel, .workspace, .gate {
  background: var(--panel);
  backdrop-filter: blur(10px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.gate {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 1.35rem 1.25rem 1.2rem;
  max-width: 560px;
  width: 100%;
  align-self: center;
}
.gate h1 {
  margin: 0 0 0.35rem;
  font-family: var(--display);
  font-size: clamp(1.6rem, 3vw, 2rem);
  letter-spacing: -0.03em;
  color: var(--teal);
}
.lede { margin: 0 0 1rem; color: var(--muted); }
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.7rem;
}
.form-grid .full { grid-column: 1 / -1; }
label {
  display: flex; flex-direction: column; gap: 0.28rem;
  font-size: 0.78rem; font-weight: 600; color: var(--muted);
}
label.check { flex-direction: row; align-items: center; gap: 0.5rem; font-weight: 500; }
input[type="text"], input[type="number"], input:not([type]), input[type="password"] {
  font: inherit; color: var(--ink);
  border: 1px solid var(--line); border-radius: 12px;
  padding: 0.65rem 0.75rem; background: #fff;
  box-shadow: inset 0 1px 2px rgba(11, 61, 58, 0.06);
}
input:focus { outline: 2px solid rgba(20, 99, 92, 0.35); border-color: var(--teal-2); }

/* 3D buttons */
.btn {
  --btn-face: #ffffff;
  --btn-edge: #c5d0cb;
  --btn-deep: #9aada4;
  --btn-ink: var(--ink);
  appearance: none;
  border: 1px solid var(--btn-edge);
  background: linear-gradient(180deg, #fff 0%, var(--btn-face) 45%, #eef3f1 100%);
  color: var(--btn-ink);
  border-radius: 12px;
  padding: 0.55rem 0.9rem;
  font: inherit; font-weight: 650;
  cursor: pointer;
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.85),
    0 2px 0 var(--btn-deep),
    0 6px 12px rgba(11, 61, 58, 0.12);
  transform: translateY(0);
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}
.btn:hover:not(:disabled) {
  filter: brightness(1.03);
  transform: translateY(-1px);
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.9),
    0 3px 0 var(--btn-deep),
    0 10px 16px rgba(11, 61, 58, 0.14);
}
.btn:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow:
    inset 0 2px 4px rgba(11, 61, 58, 0.18),
    0 1px 0 var(--btn-deep),
    0 2px 6px rgba(11, 61, 58, 0.1);
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; filter: grayscale(0.2); }
.btn.primary {
  --btn-face: #127068;
  --btn-edge: #0a3532;
  --btn-deep: var(--teal-deep);
  --btn-ink: #f4faf8;
  background: linear-gradient(180deg, #1c8a7f 0%, var(--teal-2) 42%, var(--teal) 100%);
  color: var(--btn-ink);
  border-color: var(--btn-edge);
  text-shadow: 0 1px 0 rgba(0,0,0,0.25);
}
.btn.primary:hover:not(:disabled) {
  background: linear-gradient(180deg, #21968a 0%, #178178 42%, var(--teal-2) 100%);
}
.btn.ghost {
  --btn-face: rgba(255,255,255,0.55);
  --btn-edge: rgba(18, 36, 31, 0.14);
  --btn-deep: rgba(18, 36, 31, 0.18);
  background: linear-gradient(180deg, rgba(255,255,255,0.75), rgba(255,255,255,0.4));
}
.btn.small { padding: 0.38rem 0.65rem; font-size: 0.8rem; border-radius: 10px; }
.btn.full { width: 100%; }

.badge {
  display: inline-flex; align-items: center;
  border-radius: 999px; padding: 0.2rem 0.6rem;
  font-size: 0.72rem; font-weight: 700; letter-spacing: 0.02em;
}
.badge.off { background: #e6ebe9; color: var(--muted); }
.badge.on { background: #d8f0e4; color: var(--ok); }
.chip {
  display: inline-flex; align-items: center;
  border-radius: 999px; padding: 0.15rem 0.5rem;
  background: rgba(11, 61, 58, 0.08); color: var(--teal);
  font-size: 0.75rem; font-weight: 700;
}
.chip.muted { background: #e8ecea; color: var(--muted); }
.hint { color: var(--muted); font-size: 0.82rem; }
.hint.tight { margin: 0 0 0.55rem; }
a { color: var(--teal-2); }

.workspace {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(220px, 280px) 1fr;
  overflow: hidden;
}
.workspace[hidden] { display: none !important; }

.rail {
  border-right: 1px solid var(--line);
  padding: 0.85rem 0.85rem;
  background: linear-gradient(180deg, rgba(11,61,58,0.04), transparent 40%);
  display: flex; flex-direction: column; gap: 0.35rem;
  min-height: 0; overflow: hidden;
}
.rail-head, .stage-head {
  display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
  flex: 0 0 auto;
}
.rail h2, .stage-head h2, .rail h3 {
  margin: 0; font-family: var(--display); font-size: 1.1rem; color: var(--teal);
}
.rail h3 { font-size: 0.95rem; margin-top: 0.85rem; }
.rail-section {
  flex: 0 0 auto;
  margin-top: auto;
  padding-top: 0.8rem;
  border-top: 1px dashed var(--line);
}
.peer-empty { margin: 0.35rem 0; color: var(--muted); font-size: 0.85rem; }
.rail-actions { display: flex; flex-wrap: wrap; gap: 0.35rem; }

.user-list {
  list-style: none; margin: 0; padding: 0;
  overflow: auto;
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  display: flex; flex-direction: column; gap: 0.35rem;
}
.user-list.dense { max-height: none; padding: 0.75rem; }
.user-list li {
  display: flex; flex-direction: row; align-items: center; gap: 0.55rem;
  padding: 0.55rem 0.6rem; border-radius: 12px;
  border: 1px solid transparent; cursor: pointer;
  background: rgba(255,255,255,0.55);
  min-width: 0;
}
.user-list li:hover { border-color: var(--line); }
.user-list li.active { border-color: rgba(196,107,58,0.45); background: var(--copper-soft); }
.user-list li.self { opacity: 0.75; cursor: default; }
.dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: #b7c4bf; flex: 0 0 auto;
}
.dot.on { background: #2f9e68; box-shadow: 0 0 0 3px rgba(47,158,104,0.18); }
.user-name {
  flex: 1; min-width: 0;
  font-size: 0.88rem; font-weight: 650;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.user-meta { min-width: 0; }
.user-meta strong { display: block; font-size: 0.88rem; }
.user-meta span { color: var(--muted); font-size: 0.72rem; }

.stage {
  display: flex; flex-direction: column;
  min-width: 0; min-height: 0;
  overflow: hidden;
}
.stage-tabs {
  flex: 0 0 auto;
  display: flex; gap: 0.25rem; padding: 0.55rem 0.75rem 0;
  border-bottom: 1px solid var(--line);
}
.tab {
  border: 0; background: transparent; color: var(--muted);
  font: inherit; font-weight: 650; padding: 0.55rem 0.85rem;
  border-radius: 12px 12px 0 0; cursor: pointer;
}
.tab.active { color: var(--teal); background: rgba(11,61,58,0.06); }
.mobile-only { display: none; }
.stage-panel {
  flex: 1 1 auto;
  display: flex; flex-direction: column;
  min-height: 0;
  overflow: hidden;
  padding: 0.65rem 0.75rem;
}
.stage-panel[hidden] { display: none !important; }
.stage-head { margin-bottom: 0.4rem; }

.feed-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.feed-jump {
  position: absolute;
  right: 0.85rem;
  bottom: 0.85rem;
  z-index: 5;
  min-width: 2.4rem;
  border-radius: 999px !important;
  font-weight: 800;
  background: linear-gradient(180deg, #1c8a7f, var(--teal)) !important;
  color: #f4faf8 !important;
  border-color: var(--teal-deep) !important;
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.3),
    0 3px 0 var(--teal-deep),
    0 10px 18px rgba(11, 61, 58, 0.28) !important;
}
.feed-jump .jump-count:not([hidden]) {
  display: inline-flex;
  margin-left: 0.2rem;
  min-width: 1.1rem;
  justify-content: center;
}
.feed {
  flex: 1 1 auto;
  min-height: 0;
  height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0.55rem; border-radius: 14px;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.5), rgba(243,247,245,0.9)),
    repeating-linear-gradient(0deg, transparent, transparent 27px, rgba(11,61,58,0.03) 28px);
  border: 1px solid var(--line);
  -webkit-overflow-scrolling: touch;
}
.msg {
  max-width: min(85%, 520px);
  margin: 0.35rem 0;
  padding: 0.55rem 0.7rem;
  border-radius: 14px;
  background: #fff;
  border: 1px solid var(--line);
  animation: rise 0.28s ease-out;
}
.msg.me { margin-left: auto; background: #e7f3f0; border-color: rgba(20,99,92,0.18); }
.msg.sys { background: transparent; border: 0; color: var(--muted); font-size: 0.82rem; padding: 0.25rem 0.2rem; }
.msg.dm { background: var(--copper-soft); border-color: rgba(196,107,58,0.25); }
.msg.file { border-color: rgba(47,158,104,0.35); }
.msg .who { font-size: 0.72rem; font-weight: 700; color: var(--teal-2); margin-bottom: 0.15rem; }
.msg .body { white-space: pre-wrap; word-break: break-word; }
.msg .when { margin-top: 0.25rem; font-size: 0.68rem; color: var(--muted); }

.emoji-bar {
  flex: 0 0 auto;
  display: flex; flex-wrap: wrap; gap: 0.25rem;
  margin: 0.35rem 0 0.15rem;
  max-height: 4.5rem;
  overflow: auto;
}
.emoji-bar button {
  border: 0; background: rgba(255,255,255,0.7);
  border-radius: 10px; font-size: 1.15rem; line-height: 1;
  padding: 0.28rem 0.35rem; cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.8), 0 1px 2px rgba(11,61,58,0.08);
}
.emoji-bar button:hover { background: var(--copper-soft); }
.emoji-bar button:active { transform: translateY(1px); }

.attach-preview {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 0.65rem;
  margin: 0.35rem 0; padding: 0.45rem 0.55rem;
  border: 1px dashed var(--line); border-radius: 12px;
  background: rgba(255,255,255,0.7); font-size: 0.82rem;
}
.attach-preview img, .attach-preview video {
  max-width: 96px; max-height: 64px; border-radius: 8px; object-fit: cover;
}
.attach-preview .grow { flex: 1; min-width: 0; }
.attach-preview .clear { border: 0; background: transparent; cursor: pointer; color: var(--err); font-weight: 700; }

.composer {
  flex: 0 0 auto;
  display: flex; gap: 0.45rem; margin-top: 0.5rem;
  align-items: center;
}
.composer input { flex: 1; min-width: 0; }

.msg .media {
  margin-top: 0.4rem; border-radius: 12px; overflow: hidden;
  max-width: min(100%, 360px); background: #0b3d3a10;
}
.msg .media img, .msg .media video {
  display: block; width: 100%; max-height: 240px; object-fit: contain; background: #0b1f1d;
}
.msg .media .cap { padding: 0.35rem 0.5rem; font-size: 0.82rem; }
.msg .media .wait { padding: 0.6rem; color: var(--muted); font-size: 0.8rem; }

.msg.file-offer .file-actions,
.msg.file-done {
  display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center; margin-top: 0.35rem;
}
.msg.file-done a.btn { text-decoration: none; }

.msg.file-offer {
  background: #fff8e8;
  border: 1px solid #e0c48a;
  border-radius: 14px;
  padding: 0.7rem 0.85rem;
}
.msg.file-offer .who { font-weight: 700; color: var(--teal); margin-bottom: 0.2rem; }
.msg.file-offer .file-actions { margin-top: 0.55rem; }
.msg.file-offer.accepted { opacity: 0.85; border-style: dashed; }
.msg.file-offer.rejected { background: #fdeceb; border-color: #e2a8a3; }
.msg.file-offer.accepted { background: #e8f6ef; border-color: #9dceb3; }

.tab.has-unread { position: relative; }
.tab-badge {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 1.2rem; height: 1.2rem; margin-left: 0.4rem;
  padding: 0 0.32rem; border-radius: 999px;
  background: #2f9e68; color: #fff;
  font-size: 0.7rem; font-weight: 700; line-height: 1;
  box-shadow: 0 0 0 2px rgba(47, 158, 104, 0.22);
}

dialog {
  border: 0; border-radius: 18px; padding: 0;
  box-shadow: var(--shadow); width: min(92vw, 420px);
  background: #fff;
}
dialog::backdrop { background: rgba(11, 61, 58, 0.35); backdrop-filter: blur(2px); }
.dialog-card { padding: 1.15rem; }
.dialog-card h2 { margin: 0 0 0.4rem; font-family: var(--display); color: var(--teal); }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 0.4rem; justify-content: flex-end; margin-top: 1rem; }

.toast {
  position: fixed; left: 50%; bottom: 1.2rem; transform: translateX(-50%);
  background: var(--teal); color: #f4faf8; padding: 0.65rem 1rem;
  border-radius: 999px; font-size: 0.85rem; font-weight: 600;
  box-shadow: var(--shadow); z-index: 40;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

@media (max-width: 860px) {
  .workspace { grid-template-columns: 1fr; }
  .rail { display: none; }
  .mobile-only { display: inline-flex; }
  .form-grid { grid-template-columns: 1fr; }
  .brand-sub { display: none; }
  .shell { padding: 0.55rem 0.65rem; gap: 0.45rem; }
  .msg .media img, .msg .media video { max-height: 200px; }
}
