@import url('https://fonts.googleapis.com/css2?family=EB+Garamond:ital,wght@0,400;0,500;0,600;1,400&display=swap');

* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #141416;
  --surface: #1e1e22;
  --surface2: #262629;
  --border: #2e2e33;
  --fg: #dadadf;
  --dim: #6e6e78;
  --accent: #fff;
  --green: #4ade80;
  --green-dim: #22633a;
  --red: #c06060;
  --radius: 14px;
  --radius-sm: 8px;
  --radius-xs: 6px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: 'EB Garamond', 'Garamond', serif;
  font-size: 17px;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  touch-action: manipulation;
}

/* ==========================================
   LANDING
   ========================================== */
.landing {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 100vh; height: 100dvh; gap: 1.1rem; padding: 1rem;
}
.landing h1 { font-size: 3.8rem; font-weight: 400; letter-spacing: 0.15em; text-transform: lowercase; }
.landing .sub { color: var(--dim); font-style: italic; margin-bottom: 2rem; font-size: 1.05rem; }

/* ==========================================
   BUTTONS & INPUTS
   ========================================== */
button, .btn {
  font-family: inherit; font-size: 0.95rem; background: var(--fg); color: var(--bg);
  border: none; padding: 0.65rem 1.8rem; cursor: pointer; letter-spacing: 0.06em;
  border-radius: var(--radius-sm); transition: opacity 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}
button:hover { opacity: 0.85; }
button:active { transform: scale(0.97); }

input, textarea, select {
  font-family: inherit; font-size: 0.95rem; background: var(--surface); color: var(--fg);
  border: 1px solid var(--border); padding: 0.65rem 1rem; outline: none; width: 100%;
  border-radius: var(--radius-sm); transition: border-color 0.15s; -webkit-appearance: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--dim); }
input::placeholder { color: var(--dim); }

/* ==========================================
   LOBBY (pre-join)
   ========================================== */
.lobby-overlay {
  position: fixed; inset: 0; background: var(--bg); display: flex;
  align-items: center; justify-content: center; z-index: 100;
  transition: opacity 0.3s ease; padding: 1rem;
}
.lobby-overlay.hiding { opacity: 0; pointer-events: none; }

.lobby-card {
  display: flex; flex-direction: column; align-items: center; gap: 0.8rem;
  max-width: 420px; width: 100%;
}
.lobby-title { font-size: 2.4rem; font-weight: 400; letter-spacing: 0.15em; }
.lobby-info { color: var(--dim); font-style: italic; font-size: 0.88rem; text-align: center; line-height: 1.5; }
.lobby-hint { font-size: 0.78rem; opacity: 0.6; }
.lobby-room { font-size: 0.85rem; color: var(--dim); letter-spacing: 0.06em; }
.lobby-room strong { color: var(--fg); }
.lobby-room-info { font-size: 0.8rem; color: var(--dim); font-style: italic; }
.lobby-names { color: var(--fg); font-style: normal; }

.lobby-preview {
  width: 100%; display: flex; flex-direction: column; gap: 0.5rem;
  margin: 0.5rem 0;
}
.lobby-cam-box {
  width: 100%; aspect-ratio: 16/9; background: var(--surface); border-radius: var(--radius);
  overflow: hidden; display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--border);
}
.lobby-cam-box video {
  width: 100%; height: 100%; object-fit: contain; transform: scaleX(-1);
}
.lobby-cam-placeholder { color: var(--dim); font-size: 0.85rem; font-style: italic; }

.lobby-mic-bar {
  width: 100%; height: 4px; background: var(--surface); border-radius: 2px; overflow: hidden;
}
.lobby-mic-level {
  height: 100%; width: 0%; background: var(--green); border-radius: 2px;
  transition: width 0.05s linear;
}

.lobby-toggles { display: flex; gap: 0.5rem; }

.lobby-devices {
  display: flex; flex-direction: column; gap: 0.4rem; width: 100%;
}
.lobby-select {
  font-size: 0.78rem; padding: 0.4rem 0.6rem; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-xs); color: var(--fg);
}

.lobby-join-row { display: flex; gap: 0.5rem; width: 100%; }
.lobby-join-row input { flex: 1; text-align: center; }
.lobby-join-btn { white-space: nowrap; }

.lobby-share {
  display: flex; gap: 0.5rem; width: 100%; margin-top: 0.3rem;
}
.lobby-share input { font-size: 0.78rem; text-align: center; color: var(--dim); }
.lobby-share button { font-size: 0.78rem; padding: 0.5rem 0.9rem; white-space: nowrap; }

/* ==========================================
   ROOM
   ========================================== */
.room { display: none; height: 100vh; height: 100dvh; flex-direction: column; }
.room.active { display: flex; animation: fadeIn 0.3s ease; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.45rem 1rem; border-bottom: 1px solid var(--border);
  flex-shrink: 0; background: var(--bg); z-index: 20;
}
.topbar .room-name { font-size: 0.82rem; color: var(--dim); letter-spacing: 0.1em; }

.topbar-actions { display: flex; gap: 0.5rem; align-items: center; }

.topbar-people {
  font-family: inherit; font-size: 0.78rem; color: var(--dim); background: none;
  border: none; padding: 0.3rem 0.5rem; cursor: pointer; letter-spacing: 0.04em;
  transition: color 0.15s;
}
.topbar-people:hover { color: var(--fg); }

.ctrl-btn {
  font-family: inherit; font-size: 0.75rem; background: var(--surface); color: var(--dim);
  border: 1px solid var(--border); padding: 0.3rem 0.65rem; cursor: pointer;
  border-radius: var(--radius-xs); transition: all 0.15s; letter-spacing: 0.04em;
  white-space: nowrap; -webkit-tap-highlight-color: transparent;
}
.ctrl-btn:hover { border-color: var(--dim); color: var(--fg); }
.ctrl-btn.active { background: var(--fg); color: var(--bg); border-color: var(--fg); }

.main-area { flex: 1; display: flex; overflow: hidden; min-height: 0; position: relative; }

/* ==========================================
   CONTROL BAR
   ========================================== */
.controlbar {
  display: flex; align-items: center; justify-content: center; gap: 0.3rem;
  padding: 0.5rem 1rem; border-top: 1px solid var(--border);
  background: var(--bg); flex-shrink: 0; z-index: 20;
}
.cb {
  display: flex; flex-direction: column; align-items: center; gap: 0.15rem;
  background: var(--surface); color: var(--dim); border: 1px solid var(--border);
  padding: 0.45rem 0.9rem; cursor: pointer; border-radius: var(--radius-sm);
  transition: all 0.15s; -webkit-tap-highlight-color: transparent; font-family: inherit;
  position: relative;
}
.cb svg { width: 20px; height: 20px; stroke-linecap: round; stroke-linejoin: round; }
.cb span { font-size: 0.6rem; letter-spacing: 0.04em; }
.cb:hover { border-color: var(--dim); color: var(--fg); }
.cb:active { transform: scale(0.95); }
.cb.active { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.cb.off { background: rgba(192,96,96,0.15); color: var(--red); border-color: rgba(192,96,96,0.3); }
.cb.leave { color: var(--red); border-color: rgba(192,96,96,0.25); }
.cb.leave:hover { background: rgba(192,96,96,0.15); border-color: rgba(192,96,96,0.4); color: #e08080; }

.chat-badge {
  position: absolute; top: -4px; right: -4px; background: var(--red);
  color: #fff; font-size: 0.55rem; min-width: 16px; height: 16px;
  border-radius: 8px; display: flex; align-items: center; justify-content: center;
  padding: 0 4px; font-family: sans-serif; font-weight: 600;
}

/* ==========================================
   PREFERENCES PANEL
   ========================================== */
.prefs-panel {
  display: flex; gap: 0.8rem; padding: 0 1rem;
  border-top: 1px solid var(--border); background: var(--surface);
  flex-shrink: 0; overflow: hidden; max-height: 0;
  transition: max-height 0.3s ease, padding 0.3s ease;
}
.prefs-panel.open { max-height: 200px; padding: 0.7rem 1rem; }

.prefs-section {
  display: flex; align-items: center; gap: 0.5rem; flex: 1; min-width: 0;
}
.prefs-section label {
  font-size: 0.72rem; color: var(--dim); flex-shrink: 0; letter-spacing: 0.04em; width: 4.5rem;
}
.pref-select {
  font-family: inherit; font-size: 0.75rem; background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); padding: 0.3rem 0.5rem; border-radius: var(--radius-xs);
  flex: 1; min-width: 0;
}
.pref-btn {
  font-family: inherit; font-size: 0.7rem; background: var(--surface2); color: var(--dim);
  border: 1px solid var(--border); padding: 0.25rem 0.6rem; border-radius: var(--radius-xs);
  cursor: pointer; white-space: nowrap; transition: all 0.15s;
}
.pref-btn:hover { color: var(--fg); border-color: var(--dim); }

.pref-meter-track {
  flex: 1; height: 4px; background: var(--bg); border-radius: 2px; overflow: hidden; min-width: 60px;
}
.pref-meter-fill { height: 100%; width: 0%; background: var(--green); border-radius: 2px; transition: width 0.05s linear; }

/* ==========================================
   CONTENT / TILES GRID
   ========================================== */
.content-area {
  flex: 1; display: flex; overflow: hidden; padding: 8px; gap: 8px; min-height: 0;
}
.content-area .presenter-main { display: none; }
.content-area .presenter-sidebar { display: none; }
.content-area .presenter-overflow { display: none; }

.tiles-container { flex: 1; display: grid; gap: 8px; overflow: hidden; min-height: 0; }
.tiles-container.grid-1 { grid-template-columns: 1fr; }
.tiles-container.grid-2 { grid-template-columns: 1fr 1fr; }
.tiles-container.grid-3 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.tiles-container.grid-4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.tiles-container.grid-many { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }

/* ==========================================
   PRESENTER MODE
   ========================================== */
.content-area.presenter {
  display: grid; grid-template-columns: 1fr auto; grid-template-rows: 1fr auto;
  gap: 8px; padding: 8px;
}
.content-area.presenter .tiles-container { display: none; }
.content-area.presenter .presenter-main {
  display: grid; grid-column: 1; grid-row: 1 / 3;
  border-radius: var(--radius); overflow: hidden; background: #0a0a0a;
  border: 1px solid var(--border); gap: 4px; padding: 4px; min-height: 0;
}
.content-area.presenter .presenter-main .video-tile {
  width: 100%; height: 100%; border-radius: var(--radius-sm); background: #000; min-height: 0;
}
.content-area.presenter .presenter-main .video-tile video {
  object-fit: contain !important; border-radius: var(--radius-sm); background: #000;
}
.presenter-main.screens-1 { grid-template-columns: 1fr; }
.presenter-main.screens-2 { grid-template-columns: 1fr 1fr; }
.presenter-main.screens-many { grid-template-columns: 1fr 1fr; grid-auto-rows: 1fr; }

.content-area.presenter .presenter-main .video-tile.pinned { box-shadow: inset 0 0 0 2px #60a0ff; }
.content-area.presenter .presenter-main .video-tile .peer-label { background: rgba(20,20,22,0.8); }

.content-area.presenter .presenter-sidebar {
  display: flex; flex-direction: column; gap: 8px; grid-column: 2; grid-row: 1; overflow-y: auto;
}
.content-area.presenter .presenter-sidebar .video-tile {
  width: 180px; height: 120px; flex-shrink: 0; border-radius: var(--radius-sm);
}
.content-area.presenter .presenter-sidebar .video-tile video { border-radius: var(--radius-sm); }
.content-area.presenter .presenter-sidebar .video-tile .avatar { font-size: 1.4rem; }
.content-area.presenter .presenter-sidebar .video-tile .peer-label { font-size: 0.68rem; padding: 0.1rem 0.4rem; bottom: 0.3rem; left: 0.3rem; }

.content-area.presenter .presenter-overflow {
  display: flex; flex-direction: row; gap: 8px; grid-column: 1 / 3; grid-row: 2;
  overflow-x: auto; justify-content: center;
}
.content-area.presenter .presenter-overflow .video-tile {
  width: 140px; height: 90px; flex-shrink: 0; border-radius: var(--radius-sm);
}
.content-area.presenter .presenter-overflow .video-tile video { border-radius: var(--radius-sm); }
.content-area.presenter .presenter-overflow .video-tile .avatar { font-size: 1.2rem; }
.content-area.presenter .presenter-overflow .video-tile .peer-label { font-size: 0.62rem; padding: 0.08rem 0.35rem; bottom: 0.25rem; left: 0.25rem; }
.content-area.presenter .presenter-overflow:empty { display: none; }

.content-area.presenter.participants-hidden .presenter-sidebar { display: none; }
.content-area.presenter.participants-hidden .presenter-overflow { display: none; }
.content-area.presenter.participants-hidden .presenter-main { grid-column: 1 / 3; grid-row: 1 / 3; }

.content-area.presenter .presenter-main .presenter-badge { display: inline-block; }

/* ==========================================
   VIDEO TILES
   ========================================== */
.video-tile {
  position: relative; background: var(--surface); overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius); transition: box-shadow 0.25s ease, opacity 0.3s ease;
  min-height: 0;
}
.video-tile.tile-enter { opacity: 0; }
.video-tile:has(.tile-dropdown.open) { overflow: visible; }

.video-tile video {
  width: 100%; height: 100%; object-fit: contain; border-radius: var(--radius); background: var(--surface);
}
.video-tile video.screen-share { object-fit: contain; background: #000; }

.video-tile .peer-label {
  position: absolute; bottom: 0.5rem; left: 0.6rem; font-size: 0.78rem;
  color: var(--fg); background: rgba(20,20,22,0.7); padding: 0.15rem 0.55rem;
  border-radius: var(--radius-xs); backdrop-filter: blur(4px);
  display: flex; align-items: center; gap: 0.35rem;
}
.video-tile .avatar { font-size: 2.2rem; color: var(--dim); letter-spacing: 0.04em; font-weight: 500; }

.video-tile.speaking { box-shadow: inset 0 0 0 2px rgba(255,255,255,0.5); }
.video-tile.is-sharing { box-shadow: inset 0 0 0 2px var(--green); }
.video-tile.is-sharing .presenter-badge { display: inline-block; }
.video-tile.pinned { box-shadow: inset 0 0 0 2px #60a0ff; }
.video-tile.video-hidden video { display: none !important; }
.video-tile.video-hidden .avatar { display: flex !important; }
.video-tile.remote-muted .peer-label::after {
  content: '(muted by you)'; font-size: 0.6rem; color: #bf6d6d; margin-left: 0.3rem;
}

/* Host indicator */
.video-tile.is-host .peer-label::before {
  content: '*'; font-size: 0.7rem; color: #f0c060; margin-right: 0.15rem;
}

.conn-status {
  position: absolute; top: 0.45rem; right: 0.55rem; font-size: 0.7rem;
  letter-spacing: 0.04em; border-radius: var(--radius-xs); padding: 0.1rem 0.4rem;
}
.conn-status.connecting { color: var(--dim); background: rgba(20,20,22,0.5); }
.conn-status.relay { color: #b0a060; background: rgba(20,20,22,0.5); }
.conn-status.failed { color: var(--red); background: rgba(40,20,20,0.6); }

.media-indicator { width: 6px; height: 6px; border-radius: 50%; background: #5a5a5a; flex-shrink: 0; }
.media-indicator.has-audio { background: var(--green); }
.media-indicator.muted { background: #bf6d6d; }

.presenter-badge {
  display: none; font-size: 0.65rem; color: var(--green);
  background: rgba(34,99,58,0.5); padding: 0.1rem 0.45rem;
  border-radius: var(--radius-xs); letter-spacing: 0.05em;
  margin-left: 0.3rem; animation: pulse-green 2s ease-in-out infinite;
}
@keyframes pulse-green { 0%,100% { opacity: 1; } 50% { opacity: 0.5; } }

/* ==========================================
   TILE MENU
   ========================================== */
.tile-menu { position: absolute; top: 0.4rem; left: 0.5rem; z-index: 10; opacity: 1; }
@media (hover: hover) {
  .tile-menu { opacity: 0; transition: opacity 0.15s; }
  .video-tile:hover .tile-menu { opacity: 1; }
}

.tile-menu-btn {
  font-family: inherit; font-size: 0.85rem; background: rgba(20,20,22,0.75); color: var(--dim);
  border: 1px solid rgba(255,255,255,0.08); width: 30px; height: 30px;
  border-radius: var(--radius-xs); cursor: pointer; backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center; padding: 0;
  line-height: 1; letter-spacing: 2px; transition: all 0.15s; -webkit-tap-highlight-color: transparent;
}
.tile-menu-btn:hover { color: var(--fg); background: rgba(30,30,34,0.9); border-color: var(--dim); }

.tile-dropdown {
  display: none; position: absolute; top: 34px; left: 0; background: var(--surface2);
  border: 1px solid var(--border); border-radius: var(--radius-sm); min-width: 130px;
  padding: 4px; backdrop-filter: blur(12px); box-shadow: 0 8px 32px rgba(0,0,0,0.5); z-index: 50;
}
.tile-dropdown.open { display: block; }

.tile-dropdown button {
  display: block; width: 100%; font-family: inherit; font-size: 0.78rem; color: var(--fg);
  background: transparent; border: none; padding: 0.4rem 0.6rem; cursor: pointer;
  border-radius: var(--radius-xs); text-align: left; letter-spacing: 0.02em; transition: background 0.1s;
  -webkit-tap-highlight-color: transparent;
}
.tile-dropdown button:hover { background: rgba(255,255,255,0.06); }
.tile-dropdown button:active { transform: none; }
.tile-dropdown button.menu-danger { color: var(--red); }
.tile-dropdown button.menu-danger:hover { background: rgba(192,96,96,0.1); }

.tile-dropdown .volume-row { display: flex; align-items: center; gap: 0.5rem; padding: 0.3rem 0.6rem; }
.tile-dropdown .volume-row label { font-size: 0.7rem; color: var(--dim); flex-shrink: 0; width: 2rem; }
.tile-dropdown input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 3px;
  background: var(--border); border-radius: 2px; outline: none; border: none; padding: 0;
}
.tile-dropdown input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 14px; height: 14px; border-radius: 50%; background: var(--fg); cursor: pointer;
}
.tile-dropdown .divider { height: 1px; background: var(--border); margin: 3px 0; }

/* ==========================================
   CHAT PANEL
   ========================================== */
.chat-panel {
  width: 300px; border-left: 1px solid var(--border);
  flex-direction: column; flex-shrink: 0; background: var(--bg);
  transform: translateX(100%); position: absolute; right: 0; top: 0; bottom: 0;
  transition: transform 0.25s ease; z-index: 15; display: flex;
}
.chat-panel.open { transform: translateX(0); }

.chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.6rem 0.8rem; border-bottom: 1px solid var(--border);
  font-size: 0.82rem; color: var(--dim); letter-spacing: 0.06em;
}
.chat-close {
  background: none; border: none; color: var(--dim); font-size: 1.3rem; padding: 0.2rem 0.5rem; cursor: pointer;
}
.chat-close:hover { color: var(--fg); }

.chat-messages {
  flex: 1; overflow-y: auto; padding: 0.8rem;
  display: flex; flex-direction: column; gap: 0.55rem;
}
.chat-msg .chat-author { font-size: 0.75rem; color: var(--dim); margin-right: 0.3rem; }
.chat-msg .chat-text { font-size: 0.9rem; word-break: break-word; }

.chat-input-row { display: flex; border-top: 1px solid var(--border); }
.chat-input-row input { border: none; border-radius: 0; flex: 1; }
.chat-input-row button { padding: 0.65rem 1rem; font-size: 0.85rem; border-radius: 0; }

/* ==========================================
   TOAST
   ========================================== */
.toast {
  position: fixed; bottom: 5rem; left: 50%; background: var(--surface2); color: var(--fg);
  padding: 0.45rem 1.3rem; font-size: 0.82rem; z-index: 200; pointer-events: none;
  border-radius: var(--radius-sm); border: 1px solid var(--border); white-space: nowrap;
  transform: translate(-50%, 10px); opacity: 0;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ==========================================
   SCROLLBAR
   ========================================== */
::-webkit-scrollbar { width: 3px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ==========================================
   MOBILE
   ========================================== */
@media (max-width: 700px) {
  .landing h1 { font-size: 2.5rem; }
  .lobby-title { font-size: 2rem; }
  .lobby-card { gap: 0.6rem; }
  .lobby-cam-box { aspect-ratio: 4/3; }

  .topbar { padding: 0.35rem 0.6rem; }
  .topbar .room-name { font-size: 0.72rem; }

  .controlbar { gap: 0.15rem; padding: 0.4rem 0.4rem; }
  .cb { padding: 0.4rem 0.55rem; }
  .cb svg { width: 18px; height: 18px; }
  .cb span { font-size: 0.55rem; }

  .prefs-panel.open { flex-direction: column; gap: 0.5rem; }

  .tiles-container.grid-2, .tiles-container.grid-3, .tiles-container.grid-4 { grid-template-columns: 1fr 1fr; }
  .tiles-container.grid-many { grid-template-columns: 1fr 1fr; }

  .video-tile .avatar { font-size: 1.6rem; }
  .video-tile .peer-label { font-size: 0.65rem; padding: 0.1rem 0.4rem; bottom: 0.3rem; left: 0.3rem; }

  .content-area.presenter { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .content-area.presenter .presenter-main { grid-column: 1; grid-row: 1; }
  .content-area.presenter .presenter-sidebar {
    grid-column: 1; grid-row: 2; flex-direction: row; overflow-x: auto; overflow-y: hidden;
  }
  .content-area.presenter .presenter-sidebar .video-tile { width: 100px; height: 70px; }
  .content-area.presenter .presenter-overflow { grid-column: 1; grid-row: 3; }
  .content-area.presenter .presenter-overflow .video-tile { width: 80px; height: 55px; }
  .content-area.presenter.participants-hidden .presenter-main { grid-column: 1; grid-row: 1 / 3; }

  .chat-panel { width: 100%; position: fixed; inset: 0; z-index: 50; border-left: none; transform: translateX(100%); }

  .tile-menu-btn { width: 26px; height: 26px; font-size: 0.72rem; }
  .tile-dropdown { min-width: 120px; }
  .tile-dropdown button { padding: 0.45rem 0.6rem; font-size: 0.72rem; }

  .toast { bottom: 4.5rem; font-size: 0.75rem; padding: 0.35rem 1rem; }

  .lobby-devices { gap: 0.3rem; }
}

@media (max-width: 380px) {
  .cb { padding: 0.35rem 0.4rem; }
  .cb svg { width: 16px; height: 16px; }
  .cb span { font-size: 0.5rem; }
  .topbar .room-name { font-size: 0.62rem; }
}
