:root {
  --bg: #ffffff;
  --bg2: #f5f5f7;
  --fg: #1c1c21;
  --muted: #6e6e7a;
  --line: #e6e6eb;
  --accent: #3b5bdb;
  --dot: #e03131;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141418;
    --bg2: #1c1c22;
    --fg: #e9e9ee;
    --muted: #8b8b98;
    --line: #2a2a32;
    --accent: #8ea2ff;
    --dot: #ff6b6b;
  }
}
:root[data-theme="dark"] {
  --bg: #141418;
  --bg2: #1c1c22;
  --fg: #e9e9ee;
  --muted: #8b8b98;
  --line: #2a2a32;
  --accent: #8ea2ff;
  --dot: #ff6b6b;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; } /* author display rules below must not undo the attribute */
html, body { height: 100%; }
body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
}
button { font: inherit; }
button, a { touch-action: manipulation; } /* no double-tap zoom on controls */
/* iOS Safari zooms the page when an input smaller than 16px gets focus. */
input { font-size: 16px; }

#theme { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 18px; padding: 2px 6px; line-height: 1; }
#theme:hover { color: var(--fg); }

/* --- app ------------------------------------------------------------------ */
body.app { display: grid; grid-template-columns: 220px 1fr; height: 100vh; height: 100dvh; overflow: hidden; }

#sidebar {
  background: var(--bg2);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 16px 10px 12px;
  min-height: 0;
}
.brand { font-weight: 600; padding: 4px 10px 14px; }
#rooms { overflow-y: auto; }
#rooms a {
  display: block;
  padding: 6px 10px;
  border-radius: 6px;
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#rooms a:hover { color: var(--fg); }
#rooms a { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; } /* long-press opens our menu, not the browser's */
#rooms a.current { background: var(--bg); color: var(--fg); }
#rooms a.unread { color: var(--fg); font-weight: 600; }
#rooms a.unread::after {
  content: "";
  display: inline-block;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--dot);
  margin-left: 8px;
  vertical-align: middle;
}
#newroom { margin: 4px 0 0; }
#newroom-toggle { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 6px 10px; font-size: 13px; white-space: nowrap; }
#newroom-toggle:hover { color: var(--fg); }
#newroom-name {
  width: 100%;
  font: inherit;
  font-size: 16px;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  outline: none;
}
#newroom-name:focus { border-color: var(--accent); }

#menu {
  position: fixed;
  z-index: 10;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .12);
}
#menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 6px 10px; border-radius: 6px; cursor: pointer; color: var(--fg); font-size: 14px; white-space: nowrap; }
#menu button:hover { background: var(--bg2); }

.me {
  margin-top: auto;
  padding: 8px 10px 0;
  font-size: 13px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.me span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me button { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 0; white-space: nowrap; }
.me button:hover { color: var(--fg); }

main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px 10px 20px;
  border-bottom: 1px solid var(--line);
  font-weight: 600;
}
#log { flex: 1; overflow-y: auto; padding: 12px 20px; }
#notice {
  border: 1px solid var(--accent);
  border-left-width: 4px;
  border-radius: 8px;
  padding: 8px 12px;
  margin: 10px 20px 0;
  background: var(--bg2);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
#notice b { color: var(--accent); margin-right: 8px; }
#earlier {
  display: block;
  margin: 0 auto 8px;
  background: none;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
}
#earlier:hover { color: var(--fg); }

.msg { margin-top: 14px; position: relative; }
.msg.cont { margin-top: 2px; }
.meta { font-size: 13px; color: var(--muted); }
.meta b { color: var(--fg); font-weight: 600; margin-right: 8px; }
.body { white-space: pre-wrap; overflow-wrap: anywhere; padding-right: 36px; }

/* reactions */
.react {
  position: absolute;
  right: 0;
  top: 0;
  background: none;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  font-size: 15px;
  padding: 2px 6px;
  line-height: 1.4;
  opacity: 0;
}
.msg:hover .react, .react:focus-visible, .react[aria-expanded="true"] { opacity: 1; }
@media (hover: none) { .react { opacity: .45; } }
.picker, .chips { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }
.picker button, .chip {
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1px 8px;
  font-size: 13px;
  cursor: pointer;
  color: var(--fg);
  line-height: 1.5;
}
.picker button { font-size: 16px; padding: 2px 7px; }
.picker button:hover, .chip:hover { border-color: var(--muted); }
.chip.mine { border-color: var(--accent); color: var(--accent); }

#composer {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 20px 16px;
  border-top: 1px solid var(--line);
}
#composer input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 16px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg2);
  color: var(--fg);
  outline: none;
}
#composer input:focus { border-color: var(--accent); }
.emoji { position: relative; }
#emoji-toggle { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 6px 8px; font-size: 13px; }
#emoji-toggle:hover, #emoji-toggle[aria-expanded="true"] { color: var(--fg); }
#emoji-list {
  position: absolute;
  right: 0;
  bottom: calc(100% + 6px);
  display: flex;
  gap: 2px;
  padding: 4px;
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 8px;
}
#emoji-list button { background: none; border: 0; font-size: 20px; cursor: pointer; opacity: .7; padding: 4px 6px; line-height: 1; }
#emoji-list button:hover { opacity: 1; }
#send {
  background: var(--accent);
  color: var(--bg);
  border: 0;
  border-radius: 8px;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}
#send:hover { opacity: .9; }

#flash {
  position: fixed;
  left: 50%;
  bottom: 84px;
  transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 13px;
  opacity: 0;
  transition: opacity .2s;
  pointer-events: none;
}
#flash.show { opacity: 1; }

@media (max-width: 700px), (max-height: 450px) {
  body.app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  #sidebar {
    flex-direction: row;
    align-items: center;
    gap: 6px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding: 8px 10px;
    overflow-x: auto;
  }
  .brand { padding: 0 8px 0 4px; }
  #rooms { display: flex; gap: 2px; overflow: visible; }
  #newroom { margin: 0; display: flex; align-items: center; }
  #newroom-name { width: 130px; }
  .me { margin-top: 0; margin-left: auto; padding: 0 0 0 8px; }
  .me span { display: none; }
  header { padding: 8px 10px 8px 16px; }
  #notice { margin: 8px 16px 0; }
  #log { padding: 8px 16px; }
  #composer { padding: 8px 12px 12px; }
  /* finger-sized targets */
  #rooms a, #newroom-toggle, .me button, #emoji-toggle, #earlier { min-height: 36px; display: inline-flex; align-items: center; }
  #rooms a { display: inline-flex; }
  #theme { min-width: 36px; min-height: 36px; }
  .react { font-size: 18px; padding: 6px 9px; }
  .body { padding-right: 44px; }
  .picker button, #emoji-list button { min-width: 42px; min-height: 38px; }
  .chip { min-height: 32px; padding: 3px 12px; }
}

/* --- login ---------------------------------------------------------------- */
body.login { display: grid; place-items: center; min-height: 100vh; min-height: 100dvh; padding: 16px; }
body.login #theme { position: fixed; top: 12px; right: 14px; }
.card { width: min(320px, 100%); display: flex; flex-direction: column; gap: 10px; margin: 0; }
.card h1 { font-size: 20px; margin: 0 0 6px; }
.card input {
  font: inherit;
  font-size: 16px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg2);
  color: var(--fg);
  outline: none;
}
.card input:focus { border-color: var(--accent); }
.card button {
  padding: 10px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: var(--bg);
  cursor: pointer;
}
.card button:disabled { opacity: .6; }
.error { color: var(--dot); font-size: 13px; min-height: 1.3em; margin: 0; }
