/* Default theme: a neutral blue-gray that follows the device's light/dark setting.
   A chat with its own background color gets these variables set inline by applyColor()
   in app.js, plus .scheme-light or .scheme-dark depending on how dark that color is. */
:root {
  color-scheme: light dark;
  --bg: hsl(215 12% 95%);
  --surface: hsl(215 12% 99%);
  --line: hsl(215 7% 84%);
  --text: hsl(215 20% 14%);
  --muted: hsl(215 10% 38%);
  --bubble: #fff;
  --mine: hsl(215 70% 89%);
  --accent: hsl(215 62% 35%);
  --on-accent: #fff;
  --good: hsl(145 60% 30%);
  --warn: hsl(32 90% 32%);
  --bad: hsl(0 65% 42%);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: hsl(215 6% 10%);
    --surface: hsl(215 6% 14%);
    --line: hsl(215 6% 27%);
    --text: hsl(215 15% 91%);
    --muted: hsl(215 10% 68%);
    --bubble: hsl(215 6% 20%);
    --mine: hsl(215 45% 27%);
    --accent: hsl(215 60% 68%);
    --on-accent: hsl(215 30% 10%);
    --good: hsl(145 50% 62%);
    --warn: hsl(38 85% 62%);
    --bad: hsl(0 80% 72%);
  }
}
/* After the media query so a dark chat color wins on a light-mode device and vice versa. */
.scheme-dark {
  color-scheme: dark;
  --good: hsl(145 50% 66%);
  --warn: hsl(38 85% 66%);
  --bad: hsl(0 85% 76%);
}
.scheme-light {
  color-scheme: light;
  --good: hsl(145 60% 26%);
  --warn: hsl(32 90% 28%);
  --bad: hsl(0 65% 38%);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}
h1, h2, h3, p { margin: 0; }
a { color: var(--accent); }
code { font-family: ui-monospace, Menlo, Consolas, monospace; }

button, .button, input, select, textarea {
  font: inherit;
  color: inherit;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}
button, .button {
  padding: 6px 12px;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  white-space: nowrap;
}
button:hover, .button:hover { border-color: var(--muted); }
button:disabled { opacity: .5; cursor: default; }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}
button.icon {
  border: 0;
  background: none;
  padding: 2px 6px;
  color: var(--muted);
  line-height: 1;
}
button.icon:hover { color: var(--text); }
input, select, textarea { padding: 6px 9px; max-width: 100%; }
input[type=checkbox], input[type=radio] { accent-color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.hint { color: var(--muted); font-size: 13px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 1px 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 12px;
  color: var(--muted);
  background: var(--surface);
  white-space: nowrap;
}

/* ---------- layout ---------- */

#app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 250px;
  grid-template-rows: minmax(0, 1fr) auto;
  height: 100vh;
  height: 100dvh;
  background: var(--bg);
  color: var(--text);
}
#main { display: flex; flex-direction: column; min-height: 0; position: relative; }
#main.dropping::after {
  content: 'Drop files to attach';
  position: absolute;
  inset: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px dashed var(--accent);
  border-radius: 12px;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  color: var(--accent);
  font-weight: 600;
  pointer-events: none;
}

#posts {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
#posts .empty { margin: auto; color: var(--muted); text-align: center; }

/* ---------- posts ---------- */

.post {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  max-width: min(680px, 88%);
}
.post.mine { align-self: flex-end; align-items: flex-end; }
.post .meta { font-size: 12px; color: var(--muted); padding: 0 6px; }
.bubble {
  position: relative;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--bubble);
  max-width: 100%;
}
.post.mine .bubble { background: var(--mine); border-color: transparent; }
.bubble .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble .files { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 6px; }
.bubble .text + .files { margin-top: 8px; }
.thumb img { display: block; height: 140px; max-width: 100%; object-fit: cover; border-radius: 8px; }
.file {
  display: inline-flex;
  gap: 8px;
  align-items: baseline;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  overflow-wrap: anywhere;
}
.file small { color: var(--muted); white-space: nowrap; }

.post.done .bubble { padding-left: 36px; }
.post.done .text { color: var(--muted); }
.check {
  position: absolute;
  left: 9px;
  top: 9px;
  width: 19px;
  height: 19px;
  border-radius: 50%;
  background: var(--good);
  color: var(--surface);
  font-size: 12px;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.post.is-hidden .bubble { border: 1px dashed var(--muted); opacity: .6; }

.under { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 0 4px; min-height: 22px; }
.post.mine .under { justify-content: flex-end; }
.chip.p3 { color: var(--bad); border-color: currentColor; font-weight: 600; }
.chip.p2 { color: var(--warn); border-color: currentColor; }
.chip.overdue { color: var(--bad); border-color: currentColor; font-weight: 600; }
.post .actions { display: inline-flex; flex-wrap: wrap; gap: 2px; }
.post .actions button {
  border: 0;
  background: none;
  padding: 1px 6px;
  font-size: 12px;
  color: var(--muted);
}
.post .actions button:hover { color: var(--text); text-decoration: underline; }
@media (hover: hover) {
  .post .actions { opacity: 0; }
  .post:hover .actions, .post:focus-within .actions { opacity: 1; }
}

/* ---------- composer ---------- */

#composer {
  border-top: 1px solid var(--line);
  background: var(--surface);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
#text { flex: 1; resize: none; max-height: 180px; min-width: 0; }
.composer-options { flex-wrap: wrap; align-items: center; font-size: 13px; color: var(--muted); }
.composer-options button, .composer-options select, .composer-options input { font-size: 13px; padding: 3px 8px; color: var(--text); }
#composer-files { display: flex; flex-wrap: wrap; gap: 4px; }
#readonly-note {
  border-top: 1px solid var(--line);
  background: var(--surface);
  padding: 12px;
  text-align: center;
  color: var(--muted);
}

/* ---------- sidebar ---------- */

#side {
  border-left: 1px solid var(--line);
  background: var(--surface);
  overflow-y: auto;
}
#side section {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}
#side h2 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
}
#side input[type=search] { width: 100%; }
#side button { font-size: 13px; padding: 3px 10px; }
#members { list-style: none; margin: 0; padding: 0; width: 100%; }
#members li, .member-row { display: flex; gap: 8px; align-items: center; padding: 2px 0; }
.member-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.role { font-size: 12px; color: var(--muted); white-space: nowrap; }

/* ---------- bottom tab bar ---------- */

#tabs {
  grid-column: 1 / -1;
  display: flex;
  align-items: stretch;
  border-top: 1px solid var(--line);
  background: var(--surface);
  min-height: 42px;
}
#tabs > button {
  border: 0;
  border-radius: 0;
  background: none;
  padding: 0 14px;
}
#tabs > button:hover { background: var(--bg); }
#new-chat { border-right: 1px solid var(--line) !important; font-weight: 600; color: var(--accent); }
#me {
  border-left: 1px solid var(--line) !important;
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--muted);
  font-size: 13px;
}
#side-toggle { display: none; border-left: 1px solid var(--line) !important; }
#tab-list { flex: 1; display: flex; overflow-x: auto; min-width: 0; }
.tab {
  display: flex;
  align-items: center;
  border-right: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  flex: none;
}
.tab.colored { background: var(--bg); }
.tab .tab-name {
  border: 0;
  border-radius: 0;
  background: none;
  padding: 0 14px;
  height: 100%;
  max-width: 190px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tab.active {
  /* Joined to the chat above it, like a sheet tab. */
  background: var(--bg);
  color: var(--text);
  margin-top: -1px;
  box-shadow: inset 0 -3px 0 var(--accent);
}
.tab.active .tab-name { font-weight: 600; }
.tab.unread .tab-name { font-weight: 700; color: var(--text); }
.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--accent);
}
.offline #me::before { content: "Offline · "; color: var(--bad); }

@media (max-width: 760px) {
  #app { grid-template-columns: minmax(0, 1fr); }
  #side {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 43px;
    width: min(280px, 85vw);
    transform: translateX(100%);
    transition: transform .15s;
    z-index: 5;
  }
  .side-open #side { transform: none; box-shadow: -8px 0 24px rgb(0 0 0 / .2); }
  #side-toggle { display: block; }
  #me { max-width: 110px; }
  #tabs > button, .tab .tab-name { padding: 0 10px; }
}
@media (prefers-reduced-motion: reduce) {
  #side { transition: none; }
}

/* ---------- dialog + toast ---------- */

dialog {
  width: min(480px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 16px 48px rgb(0 0 0 / .3);
}
dialog::backdrop { background: rgb(0 0 0 / .4); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 12px; }
.dlg-head h2 { font-size: 18px; overflow-wrap: anywhere; }
dialog h3 { font-size: 14px; margin: 16px 0 4px; }
.dlg-form { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.dlg-form label, .dlg-form .field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.dlg-form label > *, .dlg-form .field > * { font-size: 15px; color: var(--text); }
.dlg-form .row { display: flex; gap: 10px; flex-wrap: wrap; }
.dlg-form .row label { flex: 1; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; }
.dlg-form.danger { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); align-items: flex-start; }
.dlg-form.danger button { color: var(--bad); }
.member-admin { margin-top: 8px; }
.history { list-style: none; margin: 10px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.history small { display: block; color: var(--muted); }
.member-row { padding: 4px 0; }
.member-row.add { margin-top: 8px; padding-top: 10px; border-top: 1px solid var(--line); }
.member-row.add select:first-child { flex: 1; min-width: 0; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 50%;
  border: 2px solid var(--surface);
  background: var(--surface);
  box-shadow: 0 0 0 1px var(--line);
}
.swatch.none { background: linear-gradient(135deg, var(--surface) 46%, var(--muted) 46% 54%, var(--surface) 54%); }
.swatch.on { box-shadow: 0 0 0 2px var(--text); }
/* "Chat's color" option: a dashed ring marks it as following the owner's choice. */
.swatch.follow { outline: 2px dashed var(--muted); outline-offset: 2px; }
.presets { display: contents; }
.custom-color { display: inline-flex; flex-direction: row !important; align-items: center; gap: 6px; }
input[type=color].swatch { appearance: none; overflow: hidden; cursor: pointer; }
input[type=color].swatch::-moz-color-swatch { border: 0; }
input[type=color].swatch::-webkit-color-swatch-wrapper { padding: 0; }
input[type=color].swatch::-webkit-color-swatch { border: 0; }
.code-display {
  font: 600 28px/1.2 ui-monospace, Menlo, Consolas, monospace;
  letter-spacing: .08em;
  text-align: center;
  padding: 16px 8px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  user-select: all;
  overflow-wrap: anywhere;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: 60px;
  transform: translateX(-50%);
  max-width: calc(100vw - 24px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  box-shadow: 0 8px 24px rgb(0 0 0 / .3);
  z-index: 1000;
}

/* ---------- sign-in page ---------- */

.login-page { display: grid; place-items: center; padding: 16px; }
.login {
  width: min(380px, 100%);
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
}
.login h1 { font-size: 22px; }
.login p { color: var(--muted); }
.login .error { color: var(--bad); font-weight: 600; }
.login label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.login input { font-size: 16px; color: var(--text); padding: 8px 10px; }
.login input[name=code] { font-family: ui-monospace, Menlo, Consolas, monospace; letter-spacing: .08em; }
.login button { padding: 9px; }

/* ---------- admin page ---------- */

.admin-page { height: auto; }
.admin-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.admin-head h1 { font-size: 20px; }
.admin { max-width: 980px; margin: 0 auto; padding: 20px; display: flex; flex-direction: column; gap: 28px; }
.admin section { display: flex; flex-direction: column; gap: 10px; }
.admin h2 { font-size: 16px; }
.inline-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.inline-form input:not([type=checkbox]) { flex: 1; min-width: 180px; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; white-space: nowrap; }
td .chip { margin-left: 6px; }
td.actions { white-space: nowrap; }
td.actions button { font-size: 13px; padding: 3px 9px; margin-right: 4px; }
tr.disabled td:first-child { color: var(--muted); }
