/* ==========================================================================
   TChat - мобилно-първи стилове
   Палитра: борово зелено върху топла хартия; един акцент, всичко останало тихо.
   ========================================================================== */

:root {
    --bg: #f3f2ee;
    --surface: #ffffff;
    --surface-2: #fbfaf8;
    --ink: #16211f;
    --ink-soft: #475653;
    --muted: #7b8a87;
    --line: #e4e2dc;
    --accent: #10655a;
    --accent-soft: #e2f1ec;
    --accent-ink: #ffffff;
    --bubble-out: #d7f0e3;
    --bubble-in: #ffffff;
    --danger: #b3261e;
    --unread: #10655a;

    --radius: 14px;
    --radius-lg: 20px;
    --header-h: 56px;
    --tabbar-h: 60px;

    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);

    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", "Helvetica Neue", Arial, sans-serif;

    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #10161a;
        --surface: #171f24;
        --surface-2: #1d262c;
        --ink: #e9eeec;
        --ink-soft: #b3c0bd;
        --muted: #8b9a97;
        --line: #263139;
        --accent: #3fb99f;
        --accent-soft: #1a2f2b;
        --accent-ink: #06231d;
        --bubble-out: #1f4a41;
        --bubble-in: #1d262c;
        --danger: #f2b8b5;
        --unread: #3fb99f;
    }
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.45;
    color: var(--ink);
    background: var(--bg);
    overscroll-behavior-y: none;
}

#app {
    height: 100dvh;
}

a {
    color: var(--accent);
    text-decoration: none;
}

button {
    font: inherit;
    color: inherit;
}

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation: none !important;
        transition: none !important;
    }
}

/* --------------------------------------------------------------- обвивка */
.app-shell {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    background: var(--bg);
}

.app-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* --------------------------------------------------------------- заглавка */
.appbar {
    display: flex;
    align-items: center;
    gap: 12px;
    height: calc(var(--header-h) + var(--safe-top));
    padding: var(--safe-top) 12px 0;
    background: var(--accent);
    color: var(--accent-ink);
    flex: 0 0 auto;
    position: sticky;
    top: 0;
    z-index: 20;
}

.appbar h1 {
    font-size: 1.15rem;
    font-weight: 600;
    letter-spacing: -0.01em;
    margin: 0;
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.appbar .icon-button {
    background: none;
    border: 0;
    color: inherit;
    width: 40px;
    height: 40px;
    display: grid;
    place-items: center;
    border-radius: 999px;
    cursor: pointer;
}

.appbar .icon-button:active {
    background: rgba(255, 255, 255, 0.16);
}

.appbar-peer {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1 1 auto;
    min-width: 0;
}

.appbar-peer .peer-name {
    font-weight: 600;
    font-size: 1rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.appbar-peer .peer-state {
    font-size: 0.76rem;
    opacity: 0.85;
    height: 1.1em;
}

/* --------------------------------------------------------------- табове долу */
.tabbar {
    flex: 0 0 auto;
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    height: calc(var(--tabbar-h) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: var(--surface);
    border-top: 1px solid var(--line);
}

.tabbar a {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    font-size: 0.7rem;
    color: var(--muted);
    position: relative;
}

.tabbar a.active {
    color: var(--accent);
    font-weight: 600;
}

.tabbar .tab-badge {
    position: absolute;
    top: 6px;
    left: 50%;
    margin-left: 4px;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--unread);
    color: var(--accent-ink);
    font-size: 0.68rem;
    font-weight: 700;
    display: grid;
    place-items: center;
}

/* --------------------------------------------------------------- търсене */
.search-bar {
    padding: 10px 12px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 10;
}

.search-bar input {
    width: 100%;
    border: 0;
    background: var(--surface-2);
    border-radius: 999px;
    padding: 10px 16px;
    font-size: 0.95rem;
    color: var(--ink);
}

.search-bar input::placeholder {
    color: var(--muted);
}

/* --------------------------------------------------------------- списъци */
.list {
    background: var(--surface);
}

.row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
    min-height: 68px;
    border: 0;
    width: 100%;
    background: none;
    text-align: left;
    cursor: pointer;
    border-bottom: 1px solid var(--line);
    color: var(--ink);
}

.row:active {
    background: var(--surface-2);
}

.row-main {
    flex: 1 1 auto;
    min-width: 0;
}

.row-title {
    display: flex;
    align-items: baseline;
    gap: 8px;
}

.row-title strong {
    font-weight: 600;
    font-size: 1rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1 1 auto;
    min-width: 0;
}

.row-time {
    font-size: 0.74rem;
    color: var(--muted);
    flex: 0 0 auto;
}

.row-sub {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.87rem;
    color: var(--muted);
    margin-top: 2px;
}

.row-sub span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1 1 auto;
}

.badge-unread {
    flex: 0 0 auto;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 999px;
    background: var(--unread);
    color: var(--accent-ink);
    font-size: 0.72rem;
    font-weight: 700;
    display: grid;
    place-items: center;
}

/* --------------------------------------------------------------- аватари */
.avatar {
    flex: 0 0 auto;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    object-fit: cover;
    background: var(--surface-2);
    position: relative;
}

.avatar-sm {
    width: 34px;
    height: 34px;
}

.avatar-wrap {
    position: relative;
    flex: 0 0 auto;
}

.online-dot {
    position: absolute;
    right: 0;
    bottom: 2px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #34c759;
    border: 2px solid var(--surface);
}

/* --------------------------------------------------------------- празни състояния */
.empty {
    display: grid;
    place-items: center;
    gap: 10px;
    padding: 64px 24px;
    text-align: center;
    color: var(--muted);
}

.empty strong {
    color: var(--ink);
    font-size: 1.05rem;
}

/* --------------------------------------------------------------- чат */
.chat-screen {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    background:
        radial-gradient(circle at 1px 1px, rgba(16, 101, 90, 0.07) 1px, transparent 0) 0 0 / 22px 22px,
        var(--bg);
}

.messages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px 10px 4px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.day-sep {
    align-self: center;
    margin: 10px 0 6px;
    padding: 3px 12px;
    border-radius: 999px;
    background: var(--surface);
    border: 1px solid var(--line);
    color: var(--ink-soft);
    font-size: 0.74rem;
}

.bubble {
    max-width: min(82%, 560px);
    padding: 7px 10px 5px;
    border-radius: var(--radius);
    background: var(--bubble-in);
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.06);
    position: relative;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

.bubble.out {
    align-self: flex-end;
    background: var(--bubble-out);
    border-bottom-right-radius: 4px;
}

.bubble.in {
    align-self: flex-start;
    border-bottom-left-radius: 4px;
}

.bubble.grouped {
    border-radius: var(--radius);
}

.bubble-sender {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--accent);
    margin-bottom: 2px;
}

.bubble-text {
    white-space: pre-wrap;
    font-size: 0.97rem;
}

.bubble-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    font-size: 0.68rem;
    color: var(--muted);
    margin-top: 2px;
}

.bubble-meta .ticks {
    display: inline-flex;
}

.bubble-meta .ticks.read {
    color: #2f7fd1;
}

.bubble img.attachment,
.bubble video.attachment {
    display: block;
    max-width: 100%;
    border-radius: 10px;
    margin-bottom: 4px;
}

.file-chip {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    border-radius: 10px;
    background: var(--surface-2);
    color: var(--ink);
}

.file-chip .file-name {
    font-size: 0.9rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-chip .file-size {
    font-size: 0.72rem;
    color: var(--muted);
}

.typing {
    align-self: flex-start;
    display: flex;
    gap: 4px;
    padding: 10px 12px;
    background: var(--bubble-in);
    border-radius: var(--radius);
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.06);
}

.typing i {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--muted);
    animation: typing-bounce 1.2s infinite;
}

.typing i:nth-child(2) { animation-delay: 0.15s; }
.typing i:nth-child(3) { animation-delay: 0.3s; }

@keyframes typing-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
    30% { transform: translateY(-4px); opacity: 1; }
}

/* --------------------------------------------------------------- поле за писане */
.composer {
    flex: 0 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 8px 10px calc(8px + var(--safe-bottom));
    background: var(--surface);
    border-top: 1px solid var(--line);
}

.composer textarea {
    flex: 1 1 auto;
    resize: none;
    border: 1px solid var(--line);
    background: var(--surface-2);
    border-radius: 20px;
    padding: 10px 14px;
    font: inherit;
    font-size: 0.97rem;
    color: var(--ink);
    max-height: 120px;
    min-height: 42px;
    line-height: 1.35;
}

.composer .round-button {
    flex: 0 0 auto;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: 0;
    display: grid;
    place-items: center;
    background: var(--surface-2);
    color: var(--ink-soft);
    cursor: pointer;
}

.composer .round-button.primary {
    background: var(--accent);
    color: var(--accent-ink);
}

.composer .round-button:disabled {
    opacity: 0.45;
    cursor: default;
}

.attachment-preview {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: var(--accent-soft);
    border-top: 1px solid var(--line);
    font-size: 0.88rem;
}

.attachment-preview span {
    flex: 1 1 auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.attachment-preview button {
    border: 0;
    background: none;
    color: var(--danger);
    cursor: pointer;
}

/* --------------------------------------------------------------- бутон "нов чат" */
.fab {
    position: fixed;
    right: 16px;
    bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px);
    width: 56px;
    height: 56px;
    border-radius: 18px;
    border: 0;
    background: var(--accent);
    color: var(--accent-ink);
    display: grid;
    place-items: center;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
    cursor: pointer;
    z-index: 15;
}

/* --------------------------------------------------------------- форми/карти */
.auth-screen {
    min-height: 100dvh;
    display: grid;
    place-items: center;
    padding: 24px 20px calc(24px + var(--safe-bottom));
    background: var(--bg);
}

.auth-card {
    width: 100%;
    max-width: 420px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    padding: 28px 22px;
}

.auth-card h1 {
    font-size: 1.5rem;
    letter-spacing: -0.02em;
    margin: 0 0 4px;
}

.auth-card p.lead {
    color: var(--muted);
    margin: 0 0 22px;
    font-size: 0.92rem;
}

.field {
    margin-bottom: 14px;
}

.field label {
    display: block;
    font-size: 0.82rem;
    color: var(--ink-soft);
    margin-bottom: 6px;
}

.field input,
.field textarea,
.field select {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--surface-2);
    font: inherit;
    font-size: 1rem;
    color: var(--ink);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 13px 16px;
    border: 0;
    border-radius: 12px;
    background: var(--accent);
    color: var(--accent-ink);
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
}

.btn:disabled {
    opacity: 0.6;
    cursor: default;
}

.btn-quiet {
    background: var(--surface-2);
    color: var(--ink);
    border: 1px solid var(--line);
}

.btn-danger {
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--line);
}

.btn-inline {
    width: auto;
    padding: 8px 14px;
    font-size: 0.9rem;
}

.notice {
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 0.9rem;
    margin-bottom: 14px;
}

.notice-error {
    background: rgba(179, 38, 30, 0.1);
    color: var(--danger);
}

.notice-info {
    background: var(--accent-soft);
    color: var(--ink);
}

.auth-footer {
    margin-top: 18px;
    text-align: center;
    font-size: 0.9rem;
    color: var(--muted);
}

.page-pad {
    padding: 16px;
}

.card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 14px;
}

.card h2 {
    font-size: 1.05rem;
    margin: 0 0 12px;
}

.muted {
    color: var(--muted);
    font-size: 0.88rem;
}

.chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    border-radius: 999px;
    background: var(--accent-soft);
    color: var(--ink);
    font-size: 0.78rem;
}

.spinner {
    width: 18px;
    height: 18px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* --------------------------------------------------------------- Blazor UI */
.loading-progress {
    position: fixed;
    inset: 0;
    margin: auto;
    width: 8rem;
    height: 8rem;
}

.loading-progress circle {
    fill: none;
    stroke: var(--line);
    stroke-width: 0.6rem;
    transform-origin: 50% 50%;
    transform: rotate(-90deg);
}

.loading-progress circle:last-child {
    stroke: var(--accent);
    stroke-dasharray: calc(3.141 * var(--blazor-load-percentage, 0%) * 0.8), 500%;
    transition: stroke-dasharray 0.05s ease-in-out;
}

.loading-progress-text {
    position: fixed;
    inset: 0;
    display: grid;
    place-items: center;
    color: var(--ink-soft);
    font-size: 0.9rem;
}

.loading-progress-text::after {
    content: "Зареждане " var(--blazor-load-percentage-text, "");
}

#blazor-error-ui {
    display: none;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1000;
    padding: 12px 16px calc(12px + var(--safe-bottom));
    background: #2b2b2b;
    color: #fff;
    font-size: 0.9rem;
}

#blazor-error-ui .dismiss {
    float: right;
    cursor: pointer;
}

.pwa-toast {
    position: fixed;
    left: 50%;
    bottom: calc(24px + var(--safe-bottom));
    transform: translate(-50%, 20px);
    opacity: 0;
    transition: opacity 0.25s, transform 0.25s;
    background: var(--ink);
    color: var(--bg);
    padding: 10px 16px;
    border-radius: 999px;
    font-size: 0.85rem;
    z-index: 1000;
}

.pwa-toast.visible {
    opacity: 1;
    transform: translate(-50%, 0);
}

/* --------------------------------------------------------------- по-широки екрани */
@media (min-width: 900px) {
    .app-shell,
    .chat-screen {
        max-width: 900px;
        margin: 0 auto;
        border-left: 1px solid var(--line);
        border-right: 1px solid var(--line);
    }
}

/* --------------------------------------------------------------- админ */
.segmented {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    gap: 4px;
    padding: 10px 12px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}

.segmented button {
    border: 1px solid var(--line);
    background: var(--surface-2);
    color: var(--ink-soft);
    border-radius: 999px;
    padding: 8px 12px;
    font-size: 0.9rem;
    cursor: pointer;
}

.segmented button.active {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-ink);
    font-weight: 600;
}

.check-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 0;
    font-size: 0.95rem;
}

.check-row input {
    width: 20px;
    height: 20px;
    accent-color: var(--accent);
}

.chip-danger {
    background: rgba(179, 38, 30, 0.12);
    color: var(--danger);
}
