:root {
  color-scheme: light;
  --ink: #292727;
  --muted: #6f6a69;
  --canvas: #f2f2f0;
  --paper: #fbfbf9;
  --line: #dedbd8;
  --line-strong: #c9c4c1;
  --accent: #9a4f53;
  --accent-dark: #7f3e42;
  --accent-soft: #eee4e4;
  --accent-ink: #ffffff;
  --warm: #a9605c;
  --danger: #8e3e44;
  --shadow: 0 24px 70px rgba(32, 29, 29, .10);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { background: var(--canvas); color: var(--ink); }
button, input, textarea { font: inherit; }
button { cursor: pointer; }
input[type="checkbox"] { accent-color: var(--accent); }
::selection { color: var(--ink); background: var(--accent-soft); }

.login-shell {
  min-height: 100%; display: grid; place-items: center; padding: 24px;
  background:
    radial-gradient(circle at 15% 15%, rgba(154,79,83,.06), transparent 28%),
    radial-gradient(circle at 88% 82%, rgba(154,79,83,.035), transparent 30%),
    var(--canvas);
}
.login-card { width: min(440px, 100%); padding: 44px; border: 1px solid var(--line); border-radius: 28px; background: rgba(251,251,249,.96); box-shadow: var(--shadow); }
.brand-mark { width: 52px; height: 52px; display: grid; place-items: center; border-radius: 17px; color: white; background: var(--accent); font-weight: 800; font-size: 22px; margin-bottom: 30px; }
.brand-mark.admin { background: var(--accent-dark); }
.eyebrow { margin: 0 0 7px; color: var(--accent); font-size: 11px; font-weight: 800; letter-spacing: .16em; }
h1, h2, p { margin-top: 0; }
.login-card h1 { font-family: Georgia, "Noto Serif SC", serif; font-size: clamp(28px, 6vw, 39px); line-height: 1.12; margin-bottom: 14px; }
.muted { color: var(--muted); }
.stack-form { display: grid; gap: 10px; margin-top: 28px; }
.stack-form label { font-size: 13px; font-weight: 700; }
input, textarea { color: var(--ink); background: var(--paper); border: 1px solid var(--line-strong); border-radius: 12px; outline: 0; }
input { min-height: 46px; padding: 0 14px; }
textarea { padding: 12px 14px; resize: none; }
input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(154,79,83,.12); }
.form-error { min-height: 20px; margin: 0; color: var(--danger); font-size: 13px; }
.primary-button, .ghost-button, .danger-button, .icon-button { border: 0; border-radius: 11px; font-weight: 750; transition: transform .12s, background .15s, opacity .15s; }
.primary-button:active, .ghost-button:active, .danger-button:active, .icon-button:active { transform: translateY(1px); }
.primary-button { min-height: 44px; padding: 0 18px; color: white; background: var(--accent); }
.primary-button:hover { background: var(--accent-dark); }
.ghost-button { min-height: 38px; padding: 0 13px; color: var(--ink); background: #efefed; }
.ghost-button:hover { background: #e6e5e2; }
.danger-button { min-height: 38px; padding: 0 13px; color: white; background: var(--danger); }
.icon-button { width: 38px; height: 38px; color: var(--ink); background: transparent; font-size: 20px; }
.icon-button:hover { background: #ecebe8; }
.quiet-link { display: block; margin-top: 24px; text-align: center; color: var(--muted); font-size: 13px; text-decoration: none; }
.quiet-link:hover { color: var(--accent); }

.user-chat-shell { height: 100%; display: grid; grid-template-rows: auto 1fr; max-width: 1100px; margin: 0 auto; background: var(--paper); border-inline: 1px solid var(--line); }
.user-topbar { min-height: 77px; display: grid; grid-template-columns: minmax(0, 1fr) auto auto; align-items: center; gap: 20px; padding: 13px 24px; border-bottom: 1px solid var(--line); }
.user-topbar h1 { margin: 0; font-family: Georgia, "Noto Serif SC", serif; font-size: 21px; }
.user-profile-summary, .counterparty-summary { min-width: 0; display: flex; align-items: center; gap: 11px; }
.user-profile-copy, .counterparty-copy { min-width: 0; }
.user-profile-copy .eyebrow { margin-bottom: 2px; }
.user-profile-copy h1, .counterparty-copy span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.counterparty-copy { display: grid; gap: 2px; }
.counterparty-copy span { font-size: 13px; font-weight: 750; }
.counterparty-copy small { color: var(--muted); font-size: 11px; }
.counterparty-copy small.online { color: var(--accent-dark); }
.user-topbar-actions { display: flex; align-items: center; gap: 8px; }
.message-alert-toggle { position: relative; display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-size: 11px; font-weight: 700; cursor: pointer; }
.message-alert-toggle input { width: 36px; height: 21px; min-height: 0; position: absolute; right: 0; margin: 0; z-index: 1; opacity: 0; cursor: pointer; }
.message-alert-slider { width: 36px; height: 21px; position: relative; flex: 0 0 36px; border: 1px solid var(--line-strong); border-radius: 99px; background: #d7d4d1; transition: background .18s, border-color .18s; }
.message-alert-slider::after { content: ""; width: 15px; height: 15px; position: absolute; left: 2px; top: 2px; border-radius: 50%; background: var(--paper); box-shadow: 0 1px 4px rgba(32,29,29,.20); transition: transform .18s; }
.message-alert-toggle input:checked + .message-alert-slider { border-color: var(--accent); background: var(--accent); }
.message-alert-toggle input:checked + .message-alert-slider::after { transform: translateX(15px); }
.message-alert-toggle input:focus-visible + .message-alert-slider { outline: 3px solid rgba(154,79,83,.22); outline-offset: 2px; }
.message-alert-toggle:has(input:disabled) { opacity: .55; cursor: wait; }
.chat-root { min-height: 0; height: 100%; }
.chat-panel { height: 100%; display: grid; grid-template-rows: auto 1fr auto auto; min-height: 0; position: relative; }
.presence-strip { min-height: 29px; display: flex; align-items: center; justify-content: center; gap: 7px; padding: 5px 14px; color: var(--muted); background: #f5f5f3; border-bottom: 1px solid var(--line); font-size: 12px; }
.presence-dot { width: 7px; height: 7px; border-radius: 99px; background: #9a9896; }
.presence-strip.online .presence-dot { background: var(--accent); box-shadow: 0 0 0 3px rgba(154,79,83,.13); }
.messages { min-height: 0; overflow-y: auto; padding: 22px clamp(14px, 4vw, 40px); display: flex; flex-direction: column; gap: 12px; background: linear-gradient(rgba(251,251,249,.97), rgba(251,251,249,.97)), radial-gradient(#e4e2df 1px, transparent 1px); background-size: auto, 18px 18px; }
.load-older { align-self: center; border: 1px solid var(--line); border-radius: 99px; padding: 7px 13px; background: var(--paper); color: var(--muted); font-size: 12px; }
.message-row { width: min(82%, 730px); align-self: flex-start; display: flex; align-items: flex-start; gap: 9px; }
.message-row.own { align-self: flex-end; flex-direction: row-reverse; }
.message-avatar { width: 34px; height: 34px; flex: 0 0 34px; display: grid; place-items: center; overflow: hidden; margin-top: 17px; border: 1px solid var(--line); border-radius: 50%; color: var(--muted); background: var(--accent-soft); font-size: 13px; font-weight: 800; }
.message-avatar img { width: 100%; height: 100%; display: block; object-fit: cover; }
.message-content { min-width: 0; flex: 1; }
.message-meta { display: flex; align-items: center; gap: 7px; margin: 0 6px 4px; color: var(--muted); font-size: 11px; }
.message-row.own .message-meta { justify-content: flex-end; }
.bubble { position: relative; padding: 10px 13px; border: 1px solid var(--line); border-radius: 7px 18px 18px 18px; background: var(--paper); box-shadow: 0 5px 16px rgba(32,29,29,.045); word-break: break-word; white-space: pre-wrap; line-height: 1.48; }
.own .bubble { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); border-radius: 18px 7px 18px 18px; }
.bubble.recalled { color: var(--muted); background: #efefed; border-style: dashed; font-style: italic; }
.own .bubble.recalled { color: rgba(255,255,255,.76); background: #74585a; }
.edited { opacity: .72; }
.message-actions { display: flex; gap: 5px; justify-content: flex-end; margin-top: 4px; }
.message-actions button { border: 0; padding: 3px 6px; border-radius: 6px; color: inherit; background: transparent; opacity: .7; font-size: 11px; }
.message-actions button:hover { opacity: 1; background: rgba(127,127,127,.15); }
.media-image { display: block; max-width: min(440px, 100%); max-height: 430px; border-radius: 10px; object-fit: contain; background: #ececea; }
.media-player { width: min(480px, 100%); max-height: 420px; display: block; border-radius: 10px; }
.audio-player { width: min(340px, 100%); }
.file-card { display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; }
.file-icon { width: 42px; height: 42px; display: grid; place-items: center; border-radius: 12px; color: var(--ink); background: rgba(255,255,255,.68); font-size: 21px; }
.file-info { min-width: 0; display: grid; gap: 2px; }
.file-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
.file-size { font-size: 11px; opacity: .72; }

.pending-area { display: flex; gap: 10px; overflow-x: auto; padding: 10px 14px; border-top: 1px solid var(--line); background: #f5f5f3; }
.pending-card { width: 110px; flex: 0 0 110px; position: relative; padding: 7px; border: 1px solid var(--line); border-radius: 13px; background: var(--paper); }
.pending-card img, .pending-card video { width: 100%; height: 72px; display: block; object-fit: cover; border-radius: 8px; background: #ececea; }
.pending-card audio { width: 100%; height: 42px; }
.pending-name { margin-top: 5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 10px; }
.pending-remove { position: absolute; right: -5px; top: -6px; width: 22px; height: 22px; border: 0; border-radius: 50%; color: white; background: var(--danger); }
.composer { display: grid; grid-template-columns: auto 1fr auto auto; align-items: end; gap: 8px; padding: 12px 14px max(12px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--paper); }
.composer textarea { min-height: 44px; max-height: 120px; }
.composer-icon { width: 44px; height: 44px; border: 1px solid var(--line); border-radius: 12px; background: #efefed; font-size: 20px; }
.mic-button { display: grid; place-items: center; padding: 0; }
.mic-icon { display: block; width: 22px; height: 22px; pointer-events: none; }
.composer-icon.recording { color: white; background: var(--danger); animation: pulse 1.2s infinite; }
.send-button { min-width: 66px; height: 44px; }
.recording-bar { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 9px 14px; color: white; background: var(--danger); font-size: 13px; }
.recording-bar button { border: 1px solid rgba(255,255,255,.5); border-radius: 8px; color: white; background: transparent; padding: 5px 9px; }
@keyframes pulse { 50% { opacity: .65; } }

.admin-shell { --sidebar-width: clamp(290px, 32vw, 360px); height: 100%; position: relative; display: grid; grid-template-columns: var(--sidebar-width) minmax(0, 1fr); }
.admin-shell.sidebar-collapsed { grid-template-columns: 0 minmax(0, 1fr); }
.conversation-sidebar { min-width: 0; overflow: hidden; display: grid; grid-template-rows: auto auto 1fr; border-right: 1px solid var(--line); background: var(--paper); }
.admin-shell.sidebar-collapsed .conversation-sidebar { visibility: hidden; opacity: 0; pointer-events: none; }
.sidebar-toggle { width: 34px; height: 34px; position: absolute; left: calc(var(--sidebar-width) - 17px); top: 24px; z-index: 10; display: grid; place-items: center; padding: 0 0 2px; border: 1px solid var(--line); border-radius: 11px; color: var(--ink); background: var(--paper); box-shadow: 0 7px 20px rgba(32,29,29,.10); font-size: 24px; line-height: 1; transition: left .2s ease, background .15s, transform .12s; }
.sidebar-toggle:hover { background: var(--accent-soft); }
.sidebar-toggle:focus-visible { outline: 3px solid rgba(154,79,83,.25); outline-offset: 2px; }
.sidebar-toggle:active { transform: translateY(1px); }
.admin-shell.sidebar-collapsed .sidebar-toggle { left: 10px; }
.admin-shell.sidebar-collapsed .conversation-header { padding-left: 56px; }
.sidebar-header { min-height: 82px; display: flex; align-items: center; justify-content: space-between; padding: 17px 18px; }
.sidebar-header h1 { margin: 0; font-family: Georgia, "Noto Serif SC", serif; font-size: 24px; }
.admin-profile-summary { min-width: 0; display: flex; align-items: center; gap: 11px; }
.admin-profile-copy { min-width: 0; }
.admin-profile-copy .eyebrow { margin-bottom: 2px; }
.admin-profile-copy h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 19px; }
.profile-edit-button { border: 0; padding: 2px 0; color: var(--accent); background: transparent; font-size: 11px; font-weight: 750; }
.profile-edit-button:hover { text-decoration: underline; }
.profile-avatar { width: 42px; height: 42px; flex: 0 0 42px; display: grid; place-items: center; overflow: hidden; border: 1px solid var(--line); border-radius: 50%; color: var(--accent-dark); background: var(--accent-soft); font-size: 14px; font-weight: 800; }
.profile-avatar-large { width: 48px; height: 48px; flex-basis: 48px; font-size: 16px; }
.profile-avatar img { width: 100%; height: 100%; display: block; object-fit: cover; }
.sidebar-tools { display: grid; gap: 10px; padding: 0 16px 14px; border-bottom: 1px solid var(--line); }
.search-input { width: 100%; }
.archive-toggle { color: var(--muted); font-size: 12px; }
.archive-toggle input { min-height: 0; vertical-align: middle; }
.message-alert-setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 2px 0; border-top: 1px solid var(--line); cursor: pointer; }
.message-alert-setting-copy { display: grid; gap: 2px; color: var(--ink); font-size: 12px; }
.message-alert-setting-copy small { color: var(--muted); font-size: 10px; font-weight: 500; }
.conversation-list { min-height: 0; overflow-y: auto; padding: 8px; }
.conversation-item { width: 100%; display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; padding: 13px 12px; border: 0; border-radius: 14px; text-align: left; color: var(--ink); background: transparent; }
.conversation-item:hover, .conversation-item.active { background: var(--accent-soft); }
.conversation-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 780; }
.conversation-public-name { grid-column: 1 / -1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 11px; }
.conversation-preview { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 12px; }
.conversation-time { color: var(--muted); font-size: 10px; }
.unread-badge { min-width: 21px; height: 21px; display: grid; place-items: center; border-radius: 99px; color: white; background: var(--warm); font-size: 11px; font-weight: 800; }
.online-pin { color: var(--accent-dark); font-size: 10px; }
.list-empty { padding: 40px 18px; text-align: center; color: var(--muted); font-size: 13px; }
.admin-workspace { min-width: 0; min-height: 0; background: var(--canvas); }
.empty-conversation { height: 100%; display: grid; place-content: center; justify-items: center; text-align: center; color: var(--muted); }
.empty-conversation h2 { color: var(--ink); margin-bottom: 5px; }
.empty-icon { width: 58px; height: 58px; display: grid; place-items: center; margin-bottom: 18px; border: 1px solid var(--line); border-radius: 20px; background: var(--paper); font-size: 27px; }
.active-conversation { height: 100%; display: grid; grid-template-rows: auto 1fr; background: var(--paper); }
.conversation-header { min-height: 82px; display: flex; align-items: center; gap: 12px; padding: 12px 18px; border-bottom: 1px solid var(--line); }
.conversation-heading { min-width: 130px; margin-right: auto; }
.conversation-heading h2 { margin: 0; font-size: 19px; }
.conversation-heading p { margin: 3px 0 0; font-size: 11px; }
.conversation-heading .private-label { color: var(--accent); }
.conversation-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 7px; }
.mobile-only { display: none; }

.modal-dialog { width: min(480px, calc(100% - 28px)); border: 1px solid var(--line); border-radius: 22px; padding: 0; color: var(--ink); background: var(--paper); box-shadow: var(--shadow); }
.modal-dialog::backdrop { background: rgba(24,24,24,.58); backdrop-filter: blur(4px); }
.modal-dialog form { margin: 0; padding: 24px; }
.dialog-heading { display: flex; align-items: start; justify-content: space-between; }
.dialog-heading h2 { margin-bottom: 8px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }
.profile-preview { display: grid; place-items: center; min-height: 96px; margin-bottom: 4px; border: 1px dashed var(--line-strong); border-radius: 16px; background: var(--canvas); }
.profile-avatar-preview { width: 72px; height: 72px; flex-basis: 72px; font-size: 24px; }
.profile-remove-button { justify-self: start; }
.profile-help { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.5; }
.toast-stack { position: fixed; top: 16px; right: 16px; z-index: 1000; display: grid; gap: 8px; max-width: min(360px, calc(100vw - 32px)); pointer-events: none; }
.toast { padding: 11px 14px; border: 1px solid var(--line); border-radius: 12px; color: var(--ink); background: var(--paper); box-shadow: 0 12px 36px rgba(32,29,29,.16); font-size: 13px; }
.toast.error { color: white; background: var(--danger); border-color: var(--danger); }
.toast.success { color: white; background: var(--accent); border-color: var(--accent); }
.message-alert-popup-stack { position: fixed; top: max(18px, env(safe-area-inset-top)); right: 18px; z-index: 1400; display: grid; gap: 10px; width: min(390px, calc(100vw - 28px)); pointer-events: none; }
.message-alert-popup { width: 100%; display: grid; grid-template-columns: 42px minmax(0, 1fr) auto; align-items: center; gap: 12px; padding: 14px; border: 1px solid rgba(154,79,83,.32); border-radius: 16px; color: var(--ink); background: var(--paper); box-shadow: 0 20px 55px rgba(32,29,29,.28); text-align: left; cursor: pointer; pointer-events: auto; animation: message-alert-in .22s ease-out; }
.message-alert-popup:hover { border-color: var(--accent); transform: translateY(-1px); }
.message-alert-popup-mark { width: 42px; height: 42px; display: grid; place-items: center; border-radius: 50%; color: white; background: var(--accent); font-weight: 800; }
.message-alert-popup-copy { min-width: 0; display: grid; gap: 4px; }
.message-alert-popup-copy strong, .message-alert-popup-copy span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-alert-popup-copy strong { font-size: 14px; }
.message-alert-popup-copy span { color: var(--muted); font-size: 13px; }
.message-alert-popup-action { color: var(--accent); font-size: 13px; font-weight: 800; }
@keyframes message-alert-in { from { opacity: 0; transform: translateY(-10px) scale(.98); } to { opacity: 1; transform: none; } }

@media (max-width: 820px) {
  .admin-shell, .admin-shell.sidebar-collapsed { grid-template-columns: 1fr; }
  .admin-shell.sidebar-collapsed .conversation-sidebar { visibility: visible; opacity: 1; pointer-events: auto; }
  .sidebar-toggle { display: none; }
  .admin-shell.sidebar-collapsed .conversation-header { padding-left: 10px; }
  .admin-workspace { display: none; }
  .admin-shell.chat-open .conversation-sidebar { display: none; }
  .admin-shell.chat-open .admin-workspace { display: block; }
  .mobile-only { display: grid; }
  .conversation-header { padding: 9px 10px; }
  .conversation-actions { position: absolute; right: 8px; top: 82px; z-index: 5; max-width: calc(100% - 16px); padding: 7px; border: 1px solid var(--line); border-radius: 12px; background: var(--paper); box-shadow: 0 8px 24px rgba(0,0,0,.10); }
  .active-conversation { position: relative; }
  .chat-root { padding-top: 67px; }
  .user-chat-shell .chat-root { padding-top: 0; }
}

@media (max-width: 560px) {
  .login-card { padding: 30px 22px; border-radius: 22px; }
  .user-chat-shell { border: 0; }
  .user-topbar { min-height: 58px; grid-template-columns: minmax(0, 1fr) minmax(0, .85fr) auto; gap: 8px; padding: 8px 10px; }
  .user-profile-summary .profile-avatar, .counterparty-summary .profile-avatar { display: none; }
  .user-profile-copy .eyebrow { display: none; }
  .user-profile-copy h1 { font-size: 15px; line-height: 1.2; }
  .user-profile-copy .profile-edit-button { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 10px; }
  .counterparty-summary { grid-column: auto; grid-row: auto; padding-top: 0; border-top: 0; }
  .counterparty-copy span, .counterparty-copy small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .counterparty-copy span { font-size: 12px; }
  .counterparty-copy small { font-size: 10px; }
  .user-topbar-actions { grid-column: auto; grid-row: auto; }
  .user-topbar .message-alert-label { display: none; }
  .user-topbar-actions .ghost-button { min-height: 36px; padding-inline: 10px; }
  .message-row { width: 89%; }
  .messages { padding-inline: 12px; }
  .composer { grid-template-columns: auto 1fr auto; }
  .composer .mic-button { grid-column: 1; grid-row: 2; }
  .composer .send-button { grid-column: 3; grid-row: 1 / span 2; height: 100%; }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --ink: #e9e9e7; --muted: #a09a98; --canvas: #121313; --paper: #191a1a;
    --line: #303132; --line-strong: #414244; --accent: #9a4f53; --accent-dark: #854247;
    --accent-soft: #332326; --accent-ink: #f8f3f3; --warm: #a9605c; --danger: #a4484e;
    --shadow: 0 24px 70px rgba(0,0,0,.28);
  }
  .login-shell { background: radial-gradient(circle at 15% 15%, rgba(154,79,83,.07), transparent 28%), var(--canvas); }
  .login-card { background: rgba(25,26,26,.96); }
  .ghost-button, .composer-icon, .presence-strip, .pending-area, .bubble.recalled { background: #242526; }
  .bubble { background: #202122; }
  .own .bubble { color: var(--accent-ink); background: var(--accent); }
  .messages { background: linear-gradient(rgba(25,26,26,.97), rgba(25,26,26,.97)), radial-gradient(#292a2b 1px, transparent 1px); background-size: auto, 18px 18px; }
  .file-icon { background: rgba(0,0,0,.18); }
}
