:root {
  --bg:          #f4f6fb;
  --bg-elev:    #ffffff;
  --panel:      #ffffff;
  --panel-soft: #f7f9fd;
  --border:     #e4e9f2;
  --border-soft: #eef1f7;
  --text:       #14233e;
  --text-soft:  #4a5876;
  --muted:      #6b7793;
  --accent:     #0b4ea2;
  --accent-hover: #0a4290;
  --accent-soft: #e6efff;
  --accent-fg:  #ffffff;
  --danger:     #c0392b;
  --self-bubble: linear-gradient(180deg, #1462c7, #0b4ea2);
  --self-fg:    #ffffff;
  --other-bubble: #ffffff;
  --other-fg:   #14233e;
  --shadow-sm:  0 1px 2px rgba(12, 26, 62, 0.06);
  --shadow-md:  0 6px 24px rgba(12, 26, 62, 0.08);
  --shadow-lg:  0 16px 48px rgba(12, 26, 62, 0.14);
  --radius-sm:  8px;
  --radius:     12px;
  --radius-lg:  18px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:          #0a1426;
    --bg-elev:    #131e36;
    --panel:      #131e36;
    --panel-soft: #182547;
    --border:     #243558;
    --border-soft: #1c2b50;
    --text:       #e7ecf6;
    --text-soft:  #a7b3cc;
    --muted:      #8290ad;
    --accent:     #4f8efc;
    --accent-hover: #6aa0ff;
    --accent-soft: #1e2f5d;
    --accent-fg:  #0a1426;
    --self-bubble: linear-gradient(180deg, #4f8efc, #2f6ee8);
    --self-fg:    #ffffff;
    --other-bubble: #1c2b50;
    --other-fg:   #e7ecf6;
    --shadow-sm:  0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md:  0 6px 24px rgba(0, 0, 0, 0.45);
    --shadow-lg:  0 16px 48px rgba(0, 0, 0, 0.55);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                 "Helvetica Neue", Arial, "Noto Sans", sans-serif,
                 "Apple Color Emoji", "Segoe UI Emoji";
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  background:
    radial-gradient(1200px 600px at 90% -10%, rgba(11, 78, 162, 0.06), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, rgba(11, 78, 162, 0.05), transparent 60%),
    var(--bg);
}

#app {
  height: 100dvh;
  display: grid;
}

/* ============ login card ============ */

#app:has(.login) {
  place-items: center;
  padding: 24px;
}

.login {
  width: min(420px, 100%);
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: left;
}

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px 28px 24px;
  box-shadow: var(--shadow-lg);
}

.brand-logo {
  display: block;
  width: 120px;
  height: 120px;
  object-fit: contain;
  margin: 0 auto 6px;
  background: #fff;
  border-radius: 50%;
  padding: 8px;
  box-shadow: var(--shadow-md), 0 0 0 1px var(--border);
}

.login h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  text-align: center;
  letter-spacing: -0.01em;
}

.login p {
  margin: 0;
  color: var(--text-soft);
  font-size: 14px;
  text-align: center;
}

.login .login-intro { color: var(--text-soft); }
.login .muted { color: var(--muted); font-size: 13px; }

.login label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--text-soft);
  font-weight: 600;
  margin-top: 6px;
}

.login input,
.composer textarea {
  font: inherit;
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--panel-soft);
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}

.login input:focus,
.composer textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--bg-elev);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.login button,
.composer button,
.topbar button {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.05s;
}

.topbar button:hover,
.login button:hover { background: var(--panel-soft); }
.topbar button:active,
.login button:active { transform: translateY(1px); }

.login button[type="submit"],
.composer button[type="submit"] {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
  padding: 12px 16px;
  font-size: 15px;
}

.login button[type="submit"]:hover,
.composer button[type="submit"]:hover { background: var(--accent-hover); }
.login button[type="submit"]:disabled,
.composer button[type="submit"]:disabled { opacity: 0.6; cursor: not-allowed; }

.login-error {
  color: var(--danger);
  margin: 0;
  font-size: 13px;
  text-align: center;
}

.login-foot {
  margin-top: 6px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
}

.login .back-to-login {
  align-self: center;
  margin-top: 6px;
}

/* ============ chat layout ============ */

#app:has(.topbar) {
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 280px 1fr;
  grid-template-areas:
    "top   top"
    "rooms msgs"
    "rooms comp";
}

.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  z-index: 5;
}

.topbar .brand-mini {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: contain;
  background: #fff;
  padding: 2px;
  box-shadow: 0 0 0 1px var(--border);
  flex-shrink: 0;
}

.topbar h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  flex: 1;
  letter-spacing: -0.01em;
}

.topbar .me-name {
  font-size: 12px;
  color: var(--text-soft);
  padding: 5px 11px;
  border-radius: 999px;
  background: var(--accent-soft);
  font-weight: 600;
}

.topbar .rooms-toggle { display: none; font-size: 18px; padding: 6px 12px; }

.topbar .enable-push,
.topbar .logout {
  font-size: 13px;
  padding: 7px 12px;
}

.topbar .logout {
  color: var(--text-soft);
}

/* ============ rooms sidebar ============ */

.rooms {
  grid-area: rooms;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.rooms .room {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  color: var(--text);
  font: inherit;
  font-weight: 500;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.rooms .room:hover { background: var(--panel-soft); }
.rooms .room.active {
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
}

.rooms .room .unread {
  float: right;
  background: var(--danger);
  color: #fff;
  border-radius: 999px;
  font-size: 11px;
  padding: 0 8px;
  line-height: 19px;
  min-width: 20px;
  text-align: center;
  font-weight: 700;
}
.rooms .room.active .unread { background: #fff; color: var(--accent); }

/* ============ messages pane ============ */

.messages {
  grid-area: msgs;
  overflow-y: auto;
  padding: 18px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  scroll-padding-bottom: 16px;
}

.msg {
  max-width: min(560px, 80%);
  padding: 8px 12px 6px;
  border-radius: 16px;
  background: var(--other-bubble);
  color: var(--other-fg);
  border: 1px solid var(--border-soft);
  word-wrap: break-word;
  box-shadow: var(--shadow-sm);
}

.msg .from {
  font-size: 12px;
  color: var(--accent);
  margin-bottom: 2px;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.msg .content {
  white-space: pre-wrap;
  font-size: 14.5px;
}

.msg .time {
  font-size: 10.5px;
  color: var(--muted);
  margin-top: 2px;
  text-align: right;
  letter-spacing: 0.02em;
}

.msg.self {
  background: var(--self-bubble);
  color: var(--self-fg);
  border-color: transparent;
  align-self: flex-end;
  border-bottom-right-radius: 6px;
}
.msg.self .from { color: rgba(255, 255, 255, 0.85); }
.msg.self .time { color: rgba(255, 255, 255, 0.7); }

.msg:not(.self) {
  border-bottom-left-radius: 6px;
}

.day-sep {
  align-self: center;
  font-size: 11px;
  color: var(--muted);
  margin: 14px 0 4px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
}

/* ============ composer ============ */

.composer {
  grid-area: comp;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 14px 14px;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  position: relative;
}

.composer.drop {
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 2px var(--accent);
}

.composer .attach-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  font-size: 18px;
  flex-shrink: 0;
}

.composer-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 180px;
  min-height: 44px;
  border-radius: 22px;
  padding: 11px 16px;
}

.composer .send-btn {
  border-radius: 22px;
  padding: 0 22px;
  height: 44px;
}

.pending-files {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.file-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 5px 5px 10px;
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12.5px;
  max-width: 100%;
}

.file-chip.uploading { opacity: 0.7; }
.file-chip.error { border-color: var(--danger); }
.file-chip-name {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.file-chip-size { color: var(--muted); font-size: 11.5px; }
.file-chip-x {
  border: 0;
  background: transparent;
  color: var(--muted);
  width: 22px;
  height: 22px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  padding: 0;
}
.file-chip-x:hover { background: var(--border); color: var(--text); }

.readonly-notice {
  grid-area: comp;
  padding: 14px;
  text-align: center;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
}

/* ============ message subject + attachments ============ */

.msg .subject {
  font-size: 13.5px;
  font-weight: 700;
  margin: 0 0 4px;
  letter-spacing: -0.005em;
  color: var(--accent);
}
.msg.self .subject { color: rgba(255,255,255,0.95); }

.msg .attachments {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 6px;
}

.msg .att {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 10px;
  text-decoration: none;
  color: inherit;
  background: var(--panel-soft);
  border: 1px solid var(--border-soft);
  font-size: 13px;
  max-width: 100%;
}
.msg.self .att {
  background: rgba(255,255,255,0.12);
  border-color: rgba(255,255,255,0.18);
}
.msg .att:hover {
  background: var(--border-soft);
}
.msg.self .att:hover {
  background: rgba(255,255,255,0.18);
}
.msg .att-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}
.msg .att-size {
  color: var(--muted);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
}
.msg.self .att-size { color: rgba(255,255,255,0.7); }

.msg .att-image {
  padding: 0;
  border-radius: 12px;
  overflow: hidden;
  display: block;
  background: var(--panel-soft);
}
.msg .att-image img {
  display: block;
  max-width: 100%;
  max-height: 320px;
  object-fit: cover;
}

/* email-feed bubbles span wider and use a paper-ish look */
.msg.email {
  max-width: min(640px, 92%);
  align-self: flex-start;
  background: var(--bg-elev);
  border-color: var(--border);
}
.msg.email .content {
  font-size: 13.5px;
  color: var(--text-soft);
  max-height: 220px;
  overflow: hidden;
  position: relative;
}

/* ============ mobile ============ */

@media (max-width: 720px) {
  #app:has(.topbar) {
    grid-template-columns: 1fr;
    grid-template-areas:
      "top"
      "msgs"
      "comp";
  }
  .topbar .rooms-toggle { display: inline-block; }
  .topbar h1 { font-size: 15px; }
  .topbar .me-name { display: none; }
  .topbar .brand-mini { display: none; }
  .rooms {
    position: fixed;
    inset: 56px 0 0 0;
    z-index: 10;
    border-right: 0;
    border-bottom: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    width: min(82vw, 300px);
    box-shadow: var(--shadow-lg);
  }
  .rooms.open { transform: translateX(0); }

  .card { padding: 26px 20px 20px; border-radius: var(--radius); }
  .brand-logo { width: 88px; height: 88px; }
  .login h1 { font-size: 19px; }
}

@media (max-width: 380px) {
  .msg { max-width: 88%; }
}

/* Reduce motion if preferred */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
