[hidden] { display: none !important; }
:root {
  --bg: #f6f7f9; --panel: #ffffff; --side: #eef0f3; --text: #1c1f24; --muted: #6b7280;
  --border: #dde1e7; --accent: #2f6fed; --accent-text: #fff; --danger: #d64545;
  --user-bubble: #2f6fed; --user-text: #fff; --code: #f0f2f5;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171b; --panel: #1c1f24; --side: #111317; --text: #e6e8eb; --muted: #9aa1ab;
    --border: #2c3038; --accent: #5b8cff; --code: #262a31; --user-bubble: #2f5fd0;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
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(--text); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--border); background: var(--panel); border-radius: 8px; padding: .45rem .8rem; }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
button.ghost { background: transparent; }
button.link { border: 0; background: none; color: var(--accent); padding: 0; }
button.icon { padding: .3rem .55rem; }
input, select, textarea { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: .5rem .65rem; }
.error { color: var(--danger); min-height: 1.2em; margin: .3rem 0; }
.badge { font-size: .75rem; padding: .05rem .45rem; border-radius: 999px; background: var(--border); }

.login { min-height: 100%; display: grid; place-items: center; padding: 1rem; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 1.6rem; width: min(360px, 100%); display: grid; gap: .8rem; }
.card h1 { margin: 0 0 .3rem; font-size: 1.4rem; }
label { display: grid; gap: .3rem; color: var(--muted); font-size: .9rem; }
label input { color: var(--text); }

.app { display: grid; grid-template-columns: 280px 1fr; height: 100%; }
.sidebar { background: var(--side); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: .8rem; gap: .6rem; min-height: 0; }
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.conv { display: flex; align-items: center; gap: .3rem; padding: .45rem .6rem; border-radius: 8px; cursor: pointer; }
.conv:hover { background: var(--border); }
.conv.active { background: var(--panel); box-shadow: 0 0 0 1px var(--border); }
.conv .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1s infinite alternate; }
.conv .acts { display: none; gap: 2px; }
.conv:hover .acts { display: flex; }
.conv .acts button { border: 0; background: none; padding: 0 .2rem; color: var(--muted); }
@keyframes pulse { from { opacity: .3; } to { opacity: 1; } }
.sidebar-foot { display: grid; gap: .45rem; border-top: 1px solid var(--border); padding-top: .6rem; }
.sidebar-foot .ghost { text-align: left; }
.storage-label { display: flex; justify-content: space-between; font-size: .8rem; color: var(--muted); }
.bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.bar > div { height: 100%; background: var(--accent); width: 0; }
.bar > div.warn { background: var(--danger); }
.me { display: flex; align-items: center; gap: .4rem; font-size: .9rem; }
.me .link { margin-left: auto; }

.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: .6rem; padding: .7rem 1rem; border-bottom: 1px solid var(--border); background: var(--panel); }
.chat-head h2 { margin: 0; font-size: 1.05rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#toggle-sidebar { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 1.2rem 1rem; display: flex; flex-direction: column; gap: 1rem; }
.msg { max-width: min(820px, 100%); width: fit-content; }
.msg.user { align-self: flex-end; background: var(--user-bubble); color: var(--user-text); padding: .55rem .85rem; border-radius: 14px 14px 4px 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant { align-self: flex-start; width: min(820px, 100%); overflow-wrap: anywhere; }
.msg.assistant pre { background: var(--code); padding: .7rem; border-radius: 8px; overflow-x: auto; }
.msg.assistant code { background: var(--code); padding: .05rem .3rem; border-radius: 4px; font-size: .9em; }
.msg.assistant pre code { padding: 0; background: none; }
.msg.assistant p { margin: .4rem 0; }
.msg .tools { display: flex; flex-wrap: wrap; gap: .3rem; margin-bottom: .3rem; }
.msg .tool { font-size: .78rem; color: var(--muted); border: 1px solid var(--border); border-radius: 6px; padding: .05rem .4rem; font-family: ui-monospace, monospace; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .status { color: var(--muted); font-style: italic; }
.msg .err { color: var(--danger); }
.msg .info { color: var(--muted); font-size: .75rem; margin-top: .2rem; }
.empty { margin: auto; color: var(--muted); text-align: center; }

.composer { display: flex; gap: .5rem; padding: .8rem 1rem; border-top: 1px solid var(--border); background: var(--panel); }
.composer textarea { flex: 1; resize: none; max-height: 40vh; }

dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--panel); color: var(--text); padding: 1rem 1.2rem; width: min(420px, calc(100% - 2rem)); }
dialog.wide { width: min(1000px, calc(100% - 2rem)); }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog header { display: flex; justify-content: space-between; align-items: center; margin-bottom: .6rem; }
dialog header h2 { margin: 0; font-size: 1.15rem; }
dialog form:not(.row) { display: grid; gap: .7rem; }
.row { display: flex; flex-wrap: wrap; gap: .5rem; }
.row input { flex: 1 1 160px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: .45rem .4rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 500; }
.perms { display: flex; flex-wrap: wrap; gap: .2rem .7rem; }
.perms label { display: inline-flex; gap: .25rem; color: var(--text); font-size: .85rem; align-items: center; }
td .actions { display: flex; gap: .3rem; flex-wrap: wrap; }
.muted { color: var(--muted); }
.small { font-size: .8rem; }
p.muted { margin: .3rem 0; font-size: .85rem; }
.toolbar { margin-bottom: .4rem; }
.toolbar input[type=search] { flex: 1 1 220px; }
#new-user-form { margin: .4rem 0; padding: .6rem; border: 1px dashed var(--border); border-radius: 10px; }
.u-login { font-weight: 600; }
.u-fio, .u-post { font-size: .82rem; }
tr.no-access td { opacity: .75; }
tr.no-access td:nth-child(3) { opacity: 1; }
.switch { display: inline-flex; align-items: center; gap: .35rem; color: var(--text); font-size: .85rem; }
#users-table thead th { position: sticky; top: 0; background: var(--panel); }
#admin-dialog .table-wrap { max-height: 60vh; overflow: auto; }
#active-table thead th { position: sticky; top: 0; background: var(--panel); }
#active-dialog .table-wrap { max-height: 60vh; overflow: auto; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(300px, 85vw); z-index: 10; transform: translateX(-100%); transition: transform .2s; }
  .app.side-open .sidebar { transform: none; box-shadow: 0 0 30px rgba(0,0,0,.3); }
  #toggle-sidebar { display: inline-block; }
}

.msg.user.queued { opacity: .55; border: 1px dashed var(--user-text); }
.msg.user.queued::after { content: " · в очереди"; font-size: .75rem; }
.msg .live { font-size: .8rem; color: var(--muted); margin-top: .3rem; font-variant-numeric: tabular-nums; animation: pulse 1.2s infinite alternate; }

.attach-bar { display: flex; flex-wrap: wrap; gap: .4rem; padding: .5rem 1rem 0; background: var(--panel); border-top: 1px solid var(--border); }
.chip { display: inline-flex; align-items: center; gap: .35rem; border: 1px solid var(--border); border-radius: 8px; padding: .2rem .25rem .2rem .45rem; font-size: .82rem; max-width: 100%; }
.chip img { width: 34px; height: 34px; object-fit: cover; border-radius: 4px; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; }
.chip.busy { opacity: .6; } .chip.bad { border-color: var(--danger); color: var(--danger); }
.chip button { border: 0; background: none; padding: 0 .25rem; color: var(--muted); }
.att-list { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .35rem; }
.att { color: inherit; font-size: .85rem; text-decoration: underline; }
.att img { max-width: 220px; max-height: 160px; border-radius: 8px; display: block; }
.chat.drop { outline: 3px dashed var(--accent); outline-offset: -8px; }
#files-dialog .table-wrap { max-height: 60vh; overflow: auto; }
.fname { word-break: break-all; }

#me-login { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }

/* sidebar must never grow past its column */
.app { grid-template-columns: 280px minmax(0, 1fr); }
.sidebar { min-width: 0; overflow: hidden; }
.sidebar-foot { min-width: 0; }
.sidebar-foot > * { min-width: 0; }
.me { min-width: 0; }
.me #me-login { flex: 1 1 auto; }
.me .badge, .me .link { flex: 0 0 auto; }

.out-files { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .5rem; }
.chip-dl { border: 1px solid var(--border); border-radius: 8px; padding: .2rem .55rem; font-size: .85rem; color: var(--text); text-decoration: none; background: var(--panel); }
.chip-dl:hover { border-color: var(--accent); }
.preview { width: 100%; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--panel); }
.preview-bar { display: flex; flex-wrap: wrap; gap: .8rem; align-items: center; padding: .35rem .6rem; font-size: .85rem; border-bottom: 1px solid var(--border); }
.preview-bar span { flex: 1; font-weight: 600; }
.preview iframe { width: 100%; height: 460px; border: 0; background: #fff; display: block; }

#disk-sub { font-size: .72rem; line-height: 1.3; }

/* ================= phones (keep this block LAST so it wins) ================= */
html, body { height: 100%; }
.app { height: 100dvh; }
.composer { align-items: flex-end; }
.composer textarea { min-width: 0; }
.composer button { flex: 0 0 auto; }
@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(300px, 85vw); z-index: 20; transform: translateX(-100%); transition: transform .2s; overflow-y: auto; }
  .app.side-open .sidebar { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.45); }
  .app.side-open::after { content: ""; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 15; }
  #toggle-sidebar { display: inline-block; }
  .chat-head { padding: .5rem .6rem; padding-top: calc(.5rem + env(safe-area-inset-top, 0px)); }
  .messages { padding: .8rem .6rem; gap: .8rem; }
  .msg, .msg.assistant { max-width: 100%; }
  .msg.user { max-width: 88%; }
  .msg .tool { max-width: 100%; }
  .msg .live { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .composer { gap: .35rem; padding: .5rem; padding-bottom: calc(.5rem + env(safe-area-inset-bottom, 0px)); }
  .composer textarea { flex: 1 1 auto; font-size: 16px; padding: .5rem .6rem; }   /* 16px: no zoom-in on focus */
  .composer button { padding: .5rem .65rem; font-size: .9rem; }
  #attach { padding: .5rem .45rem; }
  .attach-bar { padding: .4rem .5rem 0; }
  .chip span { max-width: 55vw; }
  .preview iframe { height: 340px; }
  .att img { max-width: 60vw; }
  input, select, textarea { font-size: 16px; }
  dialog, dialog.wide { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; padding: .8rem; }
  #admin-dialog .table-wrap, #files-dialog .table-wrap, #active-dialog .table-wrap { max-height: none; }
  .toolbar > * { flex: 1 1 auto; }
}

button.danger-outline { color: var(--danger); border-color: var(--danger); background: transparent; }
