:root {
  --ink: #191919;
  --muted: #888;
  --green: #07c160;
  --line: #dedede;
  --surface: #ededed;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  color: var(--ink);
  background: var(--surface);
  font-synthesis: none;
}
* { box-sizing: border-box; }
body { margin: 0; }
button, input, textarea, select { font: inherit; }
button, a, input, textarea { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; border: 0; color: inherit; background: none; }
button:disabled { cursor: not-allowed; opacity: .45; }
button:focus-visible, a:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }
input, textarea, select { outline: none; }
input:focus-visible, textarea:focus-visible, select:focus-visible { border-color: var(--green); }
a { color: inherit; text-decoration: none; }
[hidden] { display: none !important; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.icon, svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.icon-button { width: 40px; height: 40px; padding: 0; display: inline-flex; align-items: center; justify-content: center; border-radius: 4px; flex-shrink: 0; }
.icon-button:hover { background: #00000008; }
.primary, .quiet-button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 20px; min-height: 42px; border-radius: 4px; font-size: 15px; }
.primary { background: var(--green); color: #fff; }
.primary:hover, .send-button:hover { background: #06ad56; }
.quiet-button { background: #f2f2f2; color: #333; }
.quiet-button:hover { background: #e9e9e9; }
.quiet-button .icon { width: 18px; height: 18px; }
.full { width: 100%; }
.text-button { color: #576b95; padding: 4px 0; }
.muted, .help { color: var(--muted); }
.help { font-size: 13px; line-height: 1.6; margin: 14px 0; }
.form-error { color: #c43737; font-size: 13px; line-height: 1.6; }
.form-error:empty { display: none; }
.danger { color: #d54242; }
label { display: block; font-size: 14px; margin-bottom: 20px; }
label > input:not([type=checkbox]), label > textarea, label > select, .password-wrap { display: block; width: 100%; margin-top: 8px; border: 1px solid #ddd; border-radius: 4px; background: #fff; }
label > input:not([type=checkbox]), label > textarea, label > select { padding: 11px 12px; color: var(--ink); font-size: 15px; min-width: 0; }
label > textarea { min-height: 90px; resize: vertical; line-height: 1.6; }
input::placeholder, textarea::placeholder { color: #aaa; }
.tag { display: inline-block; padding: 3px 6px; border-radius: 3px; background: #f2f2f2; color: #777; font-size: 12px; white-space: nowrap; }

/* Login */
.auth-view { min-height: 100dvh; display: grid; place-items: center; background: #fff; padding: 32px 24px; }
.auth-card { width: 100%; max-width: 340px; }
.auth-heading { margin-bottom: 40px; }
.auth-heading h1 { margin: 0 0 14px; font-size: 26px; font-weight: 600; }
.auth-heading p { margin: 0; color: var(--muted); font-size: 14px; }
.password-wrap { display: flex; align-items: center; padding-right: 12px; }
.password-wrap:focus-within { border-color: var(--green); }
.password-wrap input { border: 0; background: none; padding: 11px 12px; min-width: 0; width: 100%; }
#show-password { min-width: 36px; font-size: 13px; white-space: nowrap; }

/* Chat list and navigation */
.app-shell { position: fixed; top: var(--viewport-top, 0px); left: 0; right: 0; display: grid; grid-template-columns: 64px 300px minmax(0, 1fr); height: var(--viewport-height, 100dvh); overflow: hidden; }
.rail { display: flex; flex-direction: column; align-items: center; padding: 22px 0 16px; gap: 14px; background: #e3e3e3; border-right: 1px solid #d6d6d6; }
.rail-button { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 5px; width: 54px; height: 54px; color: #666; }
.rail-button span { font-size: 11px; }
.rail-button.selected { color: #07a64e; }
#settings-button { margin-top: auto; }
.sidebar { display: flex; flex-direction: column; min-width: 0; min-height: 0; border-right: 1px solid var(--line); background: #fff; }
.sidebar-heading { display: flex; align-items: center; justify-content: space-between; min-height: 60px; padding: 0 14px 0 20px; background: var(--surface); }
.sidebar-heading h1 { font-size: 18px; font-weight: 600; margin: 0; }
.search-area { padding: 0 12px 12px; background: var(--surface); }
.search-wrap { display: flex; align-items: center; gap: 7px; padding: 7px 10px; border-radius: 5px; background: #fff; }
.search-wrap .icon { width: 16px; height: 16px; color: #999; }
.search-wrap input { border: 0; background: none; color: var(--ink); min-width: 0; width: 100%; font-size: 14px; }
.character-list { flex: 1; overflow-y: auto; min-height: 0; }
.character-row { position: relative; width: 100%; display: flex; text-align: left; align-items: center; gap: 12px; padding: 13px 16px; min-height: 78px; }
.character-row::after { content: ""; position: absolute; left: 78px; right: 0; bottom: 0; height: 1px; background: #eee; }
.character-row:hover { background: #f5f5f5; }
.character-row.active { background: #ddd; }
.avatar { width: 48px; height: 48px; border-radius: 5px; overflow: hidden; display: grid; place-items: center; flex-shrink: 0; color: #4d5b50; font-size: 22px; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.small { width: 40px; height: 40px; border-radius: 4px; font-size: 19px; }
.avatar.big { width: 64px; height: 64px; border-radius: 6px; font-size: 28px; }
.character-info { min-width: 0; flex: 1; }
.character-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.character-name { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.character-time { font-size: 11px; color: #aaa; white-space: nowrap; }
.character-preview { font-size: 13px; color: #999; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 20px; }
.unread { position: absolute; left: 54px; top: 6px; background: #fa5151; color: #fff; font-size: 11px; border-radius: 12px; min-width: 18px; height: 18px; display: grid; place-items: center; padding: 0 4px; }
.list-empty { text-align: center; padding: 40px 18px; color: var(--muted); font-size: 14px; line-height: 1.6; }

/* Conversation */
.conversation { min-width: 0; min-height: 0; background: var(--surface); }
.welcome { height: 100%; display: grid; place-items: center; color: #aaa; font-size: 15px; }
.chat-panel { height: 100%; display: flex; flex-direction: column; min-height: 0; }
.chat-header { min-height: 60px; padding: 0 20px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 8px; }
.header-person { padding: 0; text-align: left; min-width: 0; flex: 1; }
.header-person h2 { font-size: 18px; margin: 0; font-weight: 600; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; }
.header-person span { display: block; font-size: 12px; color: var(--muted); margin-top: 3px; }
.header-person span:empty { display: none; }
.mobile-back { display: none; }
.mode-banner { font-size: 13px; color: #776234; background: #fff8e4; padding: 10px 20px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.mode-banner button { text-decoration: underline; white-space: nowrap; }
.message-scroller { flex: 1; min-height: 0; overflow-y: auto; padding: 20px 24px 12px; scroll-behavior: auto; overscroll-behavior: contain; overflow-anchor: none; }
.date-label { font-size: 12px; color: #999; text-align: center; margin: 6px 0 22px; }
.message { display: flex; gap: 12px; margin: 0 0 20px; align-items: flex-start; }
.message.user { flex-direction: row-reverse; }
.message-content { max-width: min(calc(100% - 100px), 570px); min-width: 0; }
.bubble { position: relative; padding: 9px 12px; border-radius: 4px; background: #fff; font-size: 16px; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere; min-height: 40px; }
.bubble::before { content: ""; position: absolute; top: 15px; left: -5px; width: 10px; height: 10px; background: inherit; transform: rotate(45deg); }
.message.user .bubble { background: #95ec69; }
.message.user .bubble::before { left: auto; right: -5px; }
.message-photo { display: block; max-width: min(230px, 100%); max-height: 320px; border-radius: 4px; object-fit: contain; cursor: zoom-in; }
.message-photo + .bubble { margin-top: 8px; }
.run-note { display: flex; align-items: center; gap: 8px; margin: 0 0 20px 52px; font-size: 12px; color: #888; }
.run-note button { color: #576b95; text-decoration: underline; white-space: nowrap; }
.run-note.error { color: #c43737; line-height: 1.6; overflow-wrap: anywhere; }
.older-button { display: block; font-size: 12px; color: #576b95; margin: 0 auto 20px; padding: 8px; }
.chat-empty { text-align: center; padding: 60px 10px; }
.chat-empty h3 { font-size: 16px; font-weight: 500; margin: 0 0 12px; }
.chat-empty p { font-size: 14px; color: var(--muted); line-height: 1.6; max-width: 340px; margin: 0 auto 18px; }
.composer-area { padding: 10px 16px; background: #f7f7f7; border-top: 1px solid var(--line); }
.composer-area form { display: flex; gap: 10px; align-items: flex-end; }
.composer-area textarea { resize: none; border: 1px solid transparent; border-radius: 4px; padding: 8px 10px; height: 40px; max-height: 150px; background: #fff; line-height: 22px; color: var(--ink); flex: 1; min-width: 0; font-size: 16px; }
.send-button { height: 40px; padding: 0 16px; background: var(--green); color: #fff; border-radius: 4px; font-size: 14px; white-space: nowrap; }
#composer-caption { font-size: 12px; color: #a35d24; margin: 8px 0 0; }
#composer-caption:empty { display: none; }
.attachment-preview { padding: 10px 16px; display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; background: #f7f7f7; }
.attachment-preview img { width: 60px; height: 60px; object-fit: cover; border-radius: 4px; }

/* Details and forms */
dialog { border: 0; border-radius: 6px; padding: 0; color: var(--ink); background: #fff; width: min(520px, calc(100% - 32px)); max-height: calc(var(--viewport-height, 100dvh) - 32px); overflow-y: auto; }
dialog::backdrop { background: #0006; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px 10px 24px; border-bottom: 1px solid #eee; position: sticky; top: 0; background: #fff; z-index: 2; }
.modal-head h2 { font-size: 18px; font-weight: 600; margin: 0; }
.modal-body { padding: 24px; }
.modal-body h3 { font-size: 15px; font-weight: 600; margin: 22px 0 12px; }
.modal-subtitle { color: var(--muted); font-size: 14px; line-height: 1.6; margin: 0 0 20px; }
.profile-hero { display: flex; align-items: center; gap: 16px; margin-bottom: 22px; }
.profile-hero h3 { font-size: 20px; margin: 0 0 8px; }
.profile-hero p { font-size: 14px; color: var(--muted); margin: 8px 0 0; }
.avatar-edit { position: relative; padding: 0; }
.avatar-edit > span { position: absolute; bottom: -4px; right: -4px; width: 23px; height: 23px; display: grid; place-items: center; background: #fff; border: 1px solid var(--line); border-radius: 4px; color: #666; }
.avatar-edit > span svg { width: 14px; height: 14px; }
.tabs { display: flex; border-bottom: 1px solid #eee; margin: 0 0 24px; }
.tabs button { flex: 1; padding: 12px 4px; font-size: 14px; color: #777; border-bottom: 2px solid transparent; }
.tabs button.active { color: #07a64e; border-color: var(--green); }
.profile-bio { font-size: 14px; line-height: 1.8; color: #555; white-space: pre-wrap; overflow-wrap: anywhere; }
.notice-box { background: #fff8e4; padding: 12px; font-size: 13px; color: #776234; line-height: 1.6; margin: 16px 0; }
.memory-card { padding: 16px 0; border-bottom: 1px solid #eee; }
.memory-card-header { display: flex; align-items: center; gap: 8px; }
.memory-card-header strong { font-size: 14px; font-weight: 500; overflow-wrap: anywhere; }
.memory-card-header button { margin-left: auto; font-size: 12px; color: #576b95; white-space: nowrap; }
.memory-card p { margin: 10px 0 6px; font-size: 14px; line-height: 1.6; color: #555; white-space: pre-wrap; overflow-wrap: anywhere; }
.memory-card small { font-size: 12px; color: #999; }
.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 0 0 16px; }
.toggle-row strong { font-size: 16px; font-weight: 500; display: block; margin-bottom: 6px; }
.toggle-row small { font-size: 13px; color: var(--muted); }
.toggle { appearance: none; margin: 0; width: 46px; height: 27px; border-radius: 18px; background: #ccc; position: relative; cursor: pointer; flex-shrink: 0; }
.toggle::before { content: ""; position: absolute; width: 23px; height: 23px; border-radius: 50%; background: #fff; left: 2px; top: 2px; }
.toggle:checked { background: var(--green); }
.toggle:checked::before { transform: translateX(19px); }
.two-columns { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
.job-card { display: flex; align-items: center; gap: 10px; padding: 12px 0; border-bottom: 1px solid #eee; font-size: 14px; }
.job-card p { margin: 0 0 5px; line-height: 1.6; overflow-wrap: anywhere; }
.job-card small { font-size: 12px; color: var(--muted); }
.job-card button { margin-left: auto; white-space: nowrap; font-size: 13px; color: #576b95; }
.settings-row { display: flex; justify-content: space-between; gap: 12px; padding: 14px 0; border-bottom: 1px solid #eee; font-size: 14px; }
.settings-row span:last-child { color: var(--muted); overflow-wrap: anywhere; }
.settings-actions { display: flex; gap: 12px; flex-wrap: wrap; }
.settings-actions > * { flex: 1; white-space: nowrap; }
.settings-about { margin-top: 24px; color: var(--muted); font-size: 14px; }
.settings-about summary { cursor: pointer; }
.plain-divider { height: 1px; background: #eee; margin: 24px 0; }
.inline-memory { padding: 16px; background: #f7f7f7; margin: 16px 0; }
.empty-note { font-size: 14px; text-align: center; padding: 24px 10px; color: var(--muted); line-height: 1.6; }
#toast { position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%); background: #333e; color: #fff; border-radius: 6px; padding: 12px 18px; font-size: 14px; opacity: 0; pointer-events: none; z-index: 10; max-width: calc(100% - 40px); line-height: 1.6; }
#toast.show { opacity: 1; }
#lightbox { background: transparent; width: auto; max-width: 95vw; max-height: 95dvh; overflow: visible; }
#lightbox::backdrop { background: #000e; }
#lightbox img { max-width: 90vw; max-height: 82dvh; display: block; object-fit: contain; }
#lightbox button { position: absolute; right: 0; top: -45px; color: #fff; }
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-thumb { background: #ccc; border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

@media (max-width: 1000px) and (min-width: 721px) {
  .app-shell { grid-template-columns: 60px 260px minmax(0, 1fr); }
  .message-scroller { padding-left: 16px; padding-right: 16px; }
}
@media (max-width: 720px) {
  .app-shell { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
  .rail { grid-row: 2; flex-direction: row; padding: 4px 0 calc(4px + env(safe-area-inset-bottom)); border: 0; border-top: 1px solid #ddd; background: #f7f7f7; gap: 0; }
  .rail-button { flex: 1; height: 48px; gap: 3px; }
  #settings-button { margin: 0; }
  .sidebar { grid-row: 1; border: 0; }
  .sidebar-heading { min-height: calc(54px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 10px 0 16px; }
  .sidebar-heading h1 { font-size: 18px; }
  .search-area { padding: 0 8px 8px; }
  .search-wrap { padding: 7px 10px; }
  .character-row { min-height: 78px; }
  .character-row.active { background: #fff; }
  .conversation { display: none; height: 100%; }
  .app-shell.chat-open { display: block; }
  .app-shell.chat-open .sidebar, .app-shell.chat-open .rail { display: none; }
  .app-shell.chat-open .conversation { display: block; }
  .chat-header { min-height: calc(54px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 6px 0; gap: 0; }
  .mobile-back { display: inline-flex; }
  .header-person { padding-left: 6px; }
  .header-person h2 { font-size: 17px; }
  .header-person span { font-size: 11px; }
  .message-scroller { padding: 16px 12px 8px; }
  .message { gap: 10px; }
  .message-content { max-width: calc(100% - 94px); }
  .message-photo { max-width: min(205px, 100%); max-height: 290px; }
  .composer-area { padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }
  .composer-area form { gap: 8px; }
  .attach-button { width: 30px; }
  .send-button { padding: 0 12px; }
  .mode-banner { padding: 9px 12px; font-size: 12px; }
  .run-note { margin-left: 50px; }
  .auth-view { place-items: start center; padding-top: max(100px, 18dvh); }
  .auth-card input { font-size: 16px; }
  dialog { width: 100%; max-width: none; top: var(--viewport-top, 0px); height: var(--viewport-height, 100dvh); max-height: var(--viewport-height, 100dvh); border-radius: 0; margin: 0; }
  .modal-head { min-height: calc(54px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 8px 0 20px; background: var(--surface); border-color: var(--line); }
  .modal-head h2 { font-size: 17px; }
  .modal-body { padding: 24px 20px calc(24px + env(safe-area-inset-bottom)); }
  .modal-body label > input:not([type=checkbox]), .modal-body label > textarea, .modal-body label > select { font-size: 16px; }
  #lightbox { width: auto; height: auto; max-height: 95dvh; margin: auto; }
}

.self-avatar-setting { display: flex; width: 100%; align-items: center; gap: 16px; padding: 0 0 22px; margin-bottom: 22px; border-bottom: 1px solid #eee; font-size: 15px; }
.self-avatar-setting span { color: #576b95; }
.install-button { margin-top: 16px; }
.stop-reply { margin-bottom: 20px; }
.message.user .message-photo { margin-left: auto; }

/* Rules and control conversations */
.mode-banner > div { display: flex; gap: 16px; }
.rule-card p, .rule-version p { white-space: pre-wrap; overflow-wrap: anywhere; }
.rule-actions { display: flex; gap: 20px; margin: 12px 0; }
.rule-card summary, .workflow-view summary { padding: 10px 0; font-size: 13px; color: #576b95; cursor: pointer; }
.rule-version { border-top: 1px solid #eee; padding: 12px 0; }
.workflow-view pre { white-space: pre-wrap; overflow-wrap: anywhere; font: 12px/1.7 monospace; max-height: 55vh; overflow-y: auto; padding: 12px; background: #f5f5f5; }
#rule-editor:not(:empty) { margin: 20px 0; padding: 16px 12px; background: #f7f7f7; }
