:root {
    --bg: #f6f1e9;
    --bg-2: #ece5d9;
    --panel: #fffdf9;
    --text: #23201c;
    --muted: #7d7468;
    --line: #e2d9cb;
    --accent: #c7623a;
    --accent-2: #a94e2c;
    --me: #d9ecff;
    --me-text: #14243a;
    --ai: #fffdf9;
    --err: #b3261e;
    --tool: #6b6256;
    --shadow: 0 1px 2px rgba(0,0,0,.08);
    --fs: 18px;
    --radius: 18px;
}
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1c1a17;
        --bg-2: #242019;
        --panel: #2a2621;
        --text: #efe9df;
        --muted: #9d9284;
        --line: #3a342c;
        --accent: #e07a4c;
        --accent-2: #f19366;
        --me: #2b4a6b;
        --me-text: #e9f2ff;
        --ai: #2a2621;
        --err: #ff8a80;
        --tool: #b3a897;
        --shadow: 0 1px 2px rgba(0,0,0,.4);
    }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
    background: var(--bg);
    color: var(--text);
    font: var(--fs)/1.45 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    -webkit-text-size-adjust: 100%;
    overscroll-behavior: none;
}
button { font: inherit; color: inherit; }
a { color: var(--accent); }

/* ---------- вход ---------- */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100dvh; padding: 16px; }
.login { width: 100%; max-width: 380px; text-align: center; }
.login-logo { width: 88px; height: 88px; border-radius: 22px; box-shadow: var(--shadow); }
.login h1 { font-size: 28px; margin: 12px 0 20px; }
.login-hint { color: var(--muted); font-size: 15px; margin: -10px 0 18px; }
.login form { display: flex; flex-direction: column; gap: 14px; text-align: left; }
.login label span { display: block; font-size: 14px; color: var(--muted); margin: 0 0 4px 6px; }
.login input {
    width: 100%; font: inherit; padding: 14px 16px; border-radius: 14px;
    border: 1px solid var(--line); background: var(--panel); color: var(--text);
}
.login button {
    margin-top: 6px; padding: 15px; border: 0; border-radius: 14px; background: var(--accent); color: #fff; font-weight: 600;
}
.login-error { background: rgba(179,38,30,.12); color: var(--err); padding: 10px 14px; border-radius: 12px; margin-bottom: 14px; }

/* ---------- чат ---------- */
.chat-page { overflow: hidden; }
#app {
    display: flex; flex-direction: column;
    height: 100dvh; height: var(--vh, 100dvh);
    max-width: 100vw;
}

.topbar {
    flex: none; display: flex; align-items: stretch; gap: 4px;
    background: var(--bg-2); border-bottom: 1px solid var(--line);
    padding: 6px 6px 0 6px; padding-top: calc(6px + env(safe-area-inset-top));
}
.tabs {
    flex: 1 1 auto; display: flex; gap: 4px; overflow-x: auto; overflow-y: hidden;
    scrollbar-width: none; -webkit-overflow-scrolling: touch; scroll-snap-type: x proximity;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
    flex: 0 0 auto; display: flex; align-items: center; gap: 6px; max-width: 220px; min-width: 96px;
    padding: 10px 8px 10px 14px; border-radius: 14px 14px 0 0; background: transparent; border: 0;
    color: var(--muted); cursor: pointer; user-select: none; -webkit-user-select: none; scroll-snap-align: start;
    -webkit-touch-callout: none;
}
.tab.active { background: var(--bg); color: var(--text); box-shadow: 0 -1px 0 var(--line), 1px 0 0 var(--line), -1px 0 0 var(--line); }
.tab-title { flex: 1 1 auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }
.tab-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; animation: pulse 1s infinite alternate; }
@keyframes pulse { from { opacity: .3 } to { opacity: 1 } }
.tab-close {
    flex: none; width: 24px; height: 24px; border-radius: 50%; border: 0; background: transparent; color: inherit;
    display: flex; align-items: center; justify-content: center; font-size: 14px; opacity: .6;
}
.tab-close:hover { background: var(--line); opacity: 1; }

.icon-btn {
    flex: none; width: 44px; height: 44px; border: 0; border-radius: 12px; background: transparent;
    font-size: 22px; line-height: 1; cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
    color: var(--muted); text-decoration: none;
}
.icon-btn:hover { background: var(--line); color: var(--text); }
.icon-btn.on { color: #fff; background: var(--accent); animation: pulse 1s infinite alternate; }
.icon-btn.busy { color: var(--muted); animation: spin 1.2s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.messages { flex: 1 1 auto; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.messages-inner { max-width: 900px; margin: 0 auto; padding: 12px 12px 8px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 92%; padding: 10px 14px; border-radius: var(--radius); box-shadow: var(--shadow); word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--me); color: var(--me-text); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--ai); border-bottom-left-radius: 6px; }
.msg.error { align-self: center; background: rgba(179,38,30,.1); color: var(--err); font-size: 15px; }
.msg.tool { align-self: flex-start; background: transparent; box-shadow: none; color: var(--tool); font-size: 14px; padding: 0 8px; font-family: ui-monospace, Menlo, Consolas, monospace; }
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant p { margin: 0 0 .6em; }
.msg.assistant pre { background: var(--bg-2); padding: 10px 12px; border-radius: 10px; overflow-x: auto; font-size: 14px; }
.msg.assistant code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .92em; }
.msg.assistant :not(pre) > code { background: var(--bg-2); padding: 1px 5px; border-radius: 5px; }
.msg.assistant table { border-collapse: collapse; font-size: .92em; }
.msg.assistant td, .msg.assistant th { border: 1px solid var(--line); padding: 4px 8px; }
.msg.assistant img { max-width: 100%; }
.msg.assistant.streaming::after { content: "▍"; color: var(--accent); animation: pulse .7s infinite alternate; }
.empty { text-align: center; color: var(--muted); margin: 30vh auto 0; font-size: 17px; }
.model-pill { align-self: center; color: var(--muted); font-size: 12px; letter-spacing: .02em; opacity: .8; padding: 2px 10px; border-radius: 10px; background: var(--bg-2); }
.empty + .model-pill, .model-pill:only-child { margin-bottom: 0; }
.drawer-user { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.2; }
.drawer-user small { font-size: 11px; opacity: .7; }

.composer {
    flex: none; display: flex; align-items: flex-end; gap: 6px; width: 100%; max-width: 916px; margin: 0 auto;
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
}
.composer-wrap { flex: none; background: var(--bg-2); border-top: 1px solid var(--line); }
.attach-strip { display: flex; gap: 8px; padding: 8px 12px 0; max-width: 916px; margin: 0 auto; width: 100%; overflow-x: auto; scrollbar-width: none; }
.attach-strip[hidden] { display: none; }
.attach-thumb { position: relative; flex: none; width: 64px; height: 64px; border-radius: 10px; overflow: hidden; box-shadow: var(--shadow); background: var(--panel); }
.attach-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.attach-thumb.uploading img { opacity: .35; }
.attach-thumb.uploading::before { content: "…"; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 22px; color: var(--muted); }
.attach-thumb button { position: absolute; top: 2px; right: 2px; width: 22px; height: 22px; border: 0; border-radius: 50%; background: rgba(0,0,0,.6); color: #fff; font-size: 13px; line-height: 22px; padding: 0; cursor: pointer; }
.msg-images { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.msg-images a { display: block; flex: 1 1 120px; max-width: 260px; border-radius: 10px; overflow: hidden; background: var(--bg-2); }
.msg-images img { width: 100%; height: auto; display: block; max-height: 320px; object-fit: cover; }
.msg.user .msg-text { white-space: pre-wrap; }
.composer textarea {
    flex: 1 1 auto; resize: none; font: inherit; line-height: 1.4; max-height: 40vh;
    padding: 11px 14px; border-radius: 22px; border: 1px solid var(--line); background: var(--panel); color: var(--text);
    outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.send-btn {
    flex: none; width: 46px; height: 46px; border: 0; border-radius: 50%; background: var(--accent); color: #fff;
    font-size: 20px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
}
.send-btn:disabled { opacity: .5; cursor: default; }
.send-btn.stop { background: var(--err); font-size: 16px; }

/* ---------- история ---------- */
.drawer {
    position: fixed; top: 0; right: 0; bottom: 0; width: min(360px, 88vw); background: var(--panel); z-index: 20;
    box-shadow: -6px 0 24px rgba(0,0,0,.25); display: flex; flex-direction: column;
    padding-top: env(safe-area-inset-top);
}
.drawer[hidden], .backdrop[hidden] { display: none; }
.drawer-head { display: flex; align-items: center; gap: 8px; padding: 10px 8px 10px 16px; border-bottom: 1px solid var(--line); }
.drawer-head b { flex: 1; font-size: 17px; }
.drawer-user { color: var(--muted); font-size: 14px; }
.drawer-logout { font-size: 14px; color: var(--muted); text-decoration: none; padding: 6px 10px; border-radius: 8px; }
.drawer-logout:hover { background: var(--line); color: var(--text); }
.drawer-actions { padding: 8px 10px; border-bottom: 1px solid var(--line); }
.drawer-export { display: block; padding: 10px 12px; border-radius: 10px; background: var(--bg-2); color: var(--text); text-decoration: none; font-size: 15px; }
.drawer-export:hover { background: var(--line); }
.drawer-list { overflow-y: auto; flex: 1; padding: 6px; }

/* ---------- календарь ---------- */
.calendar-page { overflow: auto; }
#cal { max-width: 720px; margin: 0 auto; padding: 8px 10px 40px; padding-top: calc(8px + env(safe-area-inset-top)); }
.cal-head { display: flex; align-items: center; gap: 4px; margin-bottom: 6px; }
.cal-head h1 { flex: 1; text-align: center; font-size: 18px; margin: 0; text-transform: capitalize; }
.cal-add { color: #fff; background: var(--accent); }
.cal-hint { font-size: 14px; color: var(--muted); background: var(--bg-2); padding: 8px 12px; border-radius: 10px; margin-bottom: 8px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 3px; }
.cal-dow { text-align: center; font-size: 12px; color: var(--muted); padding: 4px 0; }
.cal-cell { aspect-ratio: 1 / 1; border: 0; border-radius: 10px; background: var(--panel); color: var(--text); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; padding: 0; cursor: pointer; font: inherit; }
.cal-cell .n { font-size: 16px; }
.cal-cell .dots { font-size: 10px; line-height: 1; color: var(--accent); letter-spacing: 1px; }
.cal-cell.other { opacity: .4; }
.cal-cell.today .n { color: var(--accent); font-weight: 700; }
.cal-cell.sel { outline: 2px solid var(--accent); }
.cal-day { margin-top: 14px; }
.cal-day h2 { font-size: 16px; margin: 0 0 8px 4px; color: var(--muted); font-weight: 600; }
.cal-list { display: flex; flex-direction: column; gap: 6px; }
.cal-item { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; width: 100%; text-align: left; padding: 10px 12px; border: 0; border-radius: 12px; background: var(--panel); color: var(--text); font: inherit; box-shadow: var(--shadow); cursor: pointer; }
.cal-item .time { color: var(--accent); font-size: 14px; white-space: nowrap; }
.cal-item .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cal-item .meta { color: var(--muted); font-size: 12px; white-space: nowrap; }
.cal-empty { color: var(--muted); padding: 16px 4px; }
.cal-modal { position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: var(--panel); border-radius: 18px 18px 0 0; padding: 14px 14px calc(14px + env(safe-area-inset-bottom)); max-height: 92dvh; overflow-y: auto; box-shadow: 0 -6px 24px rgba(0,0,0,.25); }
.cal-modal[hidden] { display: none; }
.cal-form { display: flex; flex-direction: column; gap: 10px; max-width: 560px; margin: 0 auto; }
.cal-form label > span { display: block; font-size: 13px; color: var(--muted); margin: 0 0 4px 4px; }
.cal-form input[type=text], .cal-form input[type=datetime-local], .cal-form select, .cal-form textarea { width: 100%; font: inherit; font-size: 16px; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
.cal-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.cal-row > label { min-width: 0; }
.cal-form input[type=datetime-local] { min-width: 0; max-width: 100%; }
@media (max-width: 480px) { .cal-row { grid-template-columns: 1fr; } }
.cal-check { display: flex; align-items: center; gap: 10px; padding: 4px; }
.cal-check input { width: 22px; height: 22px; accent-color: var(--accent); }
.cal-actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.cal-actions .grow { flex: 1; }
.cal-btn { padding: 11px 16px; border: 1px solid var(--line); border-radius: 12px; background: var(--bg-2); color: var(--text); font: inherit; cursor: pointer; }
.cal-btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.cal-btn.danger { color: var(--err); }
@media (min-width: 720px) { .cal-modal { left: 50%; right: auto; bottom: auto; top: 50%; transform: translate(-50%, -50%); width: 600px; border-radius: 18px; } }
.tg-ok { color: #2c7a3d; margin: 4px; }
.tg-how { margin: 4px; font-size: 15px; }
.tg-code { display: inline-block; margin: 4px; padding: 8px 12px; border-radius: 10px; background: var(--bg-2); font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 16px; user-select: all; }

/* ---------- профиль ---------- */
.profile-page { overflow: auto; }
.profile { max-width: 640px; margin: 0 auto; padding: 12px 16px 40px; }
.profile-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.profile-head h1 { font-size: 22px; margin: 0; flex: 1; }
.profile-login { color: var(--muted); font-size: 14px; }
.profile-ok { background: rgba(60,160,80,.15); color: #2c7a3d; padding: 10px 14px; border-radius: 12px; margin-bottom: 12px; font-size: 15px; }
.profile form { display: flex; flex-direction: column; gap: 16px; }
.profile label > span, .profile legend { display: block; font-size: 14px; color: var(--muted); margin: 0 0 6px 4px; }
.profile input[type=text], .profile textarea { width: 100%; font: inherit; padding: 12px 14px; border-radius: 14px; border: 1px solid var(--line); background: var(--panel); color: var(--text); resize: vertical; }
.profile textarea.mono { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 14px; }
.profile fieldset { border: 1px solid var(--line); border-radius: 14px; padding: 10px 14px 12px; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.profile .radio { display: flex; align-items: center; gap: 10px; padding: 6px 4px; }
.profile .radio input { width: 22px; height: 22px; accent-color: var(--accent); }
.profile button { padding: 15px; border: 0; border-radius: 14px; background: var(--accent); color: #fff; font-weight: 600; font: inherit; }
.hist {
    display: flex; align-items: center; gap: 6px; width: 100%; text-align: left; padding: 10px 6px 10px 12px;
    border: 0; background: transparent; border-radius: 10px; cursor: pointer;
}
.hist:hover { background: var(--bg-2); }
.hist-title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hist-date { color: var(--muted); font-size: 13px; }
.hist-del { color: var(--muted); border: 0; background: transparent; width: 32px; height: 32px; border-radius: 8px; cursor: pointer; }
.hist-del:hover { color: var(--err); background: var(--line); }
.drawer-empty { color: var(--muted); text-align: center; padding: 30px 10px; }
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 19; }

@media (min-width: 720px) {
    :root { --fs: 17px; }
    .tab { max-width: 260px; }
    .msg { max-width: 82%; }
}

.msg-doc { display: inline-block; padding: 8px 12px; border-radius: 10px; background: rgba(0,0,0,.06); color: inherit; text-decoration: none; font-size: 15px; }
