@font-face {
    font-family: 'Fira Code';
    src: url('../fonts/FiraCode.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}

@font-face {
    font-family: 'Font', monospace;
    src: url('../fonts/Font.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}

:root {
    --accent: #30ba8f;
    --accent-hover: #3fd6a6;
    --accent-rgb: 48, 186, 143;
    --on-accent: #000;            /* tekst na przyciskach w kolorze akcentu */

    /* Motyw ciemny — domyślny. Jasny nadpisuje te tokeny w @media niżej. */
    --bg: #000000;                /* tło strony */
    --text: #f5f5f5;              /* tekst podstawowy */
    --muted: #9a9a9a;             /* tekst drugoplanowy (opisy komend) */
    --surface: #0a0a0a;           /* paski i lista podpowiedzi (warstwa nad tłem) */
    --divider: #1c1c1c;           /* separatory paska i composera */
    --bubble-guest-bg: #1a1a1a;
    --bubble-guest-border: #333;
    --input-bg: #000;
    --input-border: #333;
    --shadow: rgba(0, 0, 0, 0.6);
    --focus-ring: #f5f5f5;
    --code-bg: rgba(255, 255, 255, 0.10); /* tło kodu/bloków w wiadomościach */

    --app-height: 100dvh; /* JS nadpisuje realną wysokością z VisualViewport (px) */

    /* Wspólna wysokość kontrolek composera = naturalna wysokość jednowierszowego
       textarea (line-height 1.4 × font + 2×1.2vh padding + 2×1px border). Pole
       tekstowe ma ją z natury; przyciski przypinają się do niej, by się zrównać. */
    --control-h: calc(1.4 * clamp(13px, 1.9vh, 16px) + 2.4vh + 2px);
}

/* Jasny motyw wybierany automatycznie wg preferencji systemowej użytkownika.
   Akcent przyciemniony, by zachować kontrast na jasnym tle. */
@media (prefers-color-scheme: light) {
    :root {
        --accent: #15815f;
        --accent-hover: #1a9c73;
        --accent-rgb: 21, 129, 95;
        --on-accent: #fff;

        --bg: #e9e9eb;
        --text: #1a1a1a;
        --muted: #5f5f5f;
        --surface: #ffffff;
        --divider: #d6d6da;
        --bubble-guest-bg: #ffffff;
        --bubble-guest-border: #cfcfd4;
        --input-bg: #ffffff;
        --input-border: #c4c4c9;
        --shadow: rgba(0, 0, 0, 0.18);
        --focus-ring: #1a1a1a;
        --code-bg: rgba(0, 0, 0, 0.06);
    }
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
}

body {
    background-color: var(--bg);
    font-family: 'Fira Code', Font, monospace;
    color: var(--text);
    margin: 0;
    /* Przypięcie body wycisza iOS rubber-band: nic nie scrolluje się pod stage'em */
    position: fixed;
    inset: 0;
    width: 100%;
}

[hidden] {
    display: none !important;
}

.main-container {
    display: none;
}

/* ===== Fullscreen chat ===== */
.chat-stage {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: var(--app-height);
}

/* Subtelne wejście szkieletu na load. main.js przełącza #content z display:none
   na flex, co odpala te animacje. Chrome = .chat-bar + .chat-composer; wieszamy je
   na dzieciach, NIE na #chat-stage — ten ma transform sterowany przez viewport.js
   (klawiatura iOS). `both` trzyma stan początkowy (opacity:0) podczas animation-delay,
   więc nie ma błysku w pełni widocznej treści przed staggerem. */
@keyframes ak-enter {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* Wejście pojedynczego dymka — tylko dla bąbelków obecnych na load (klasa
   .bubble-enter dokładana w JS). Bazowy .bubble zostaje bez animacji, żeby nie ruszać
   efektu pisania odbieranych wiadomości. `both` trzyma opacity:0 w trakcie
   animation-delay (start za chrome), więc nie ma błysku. */
@keyframes ak-bubble-in {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

.chat-bar {
    display: flex;
    align-items: center;
    gap: 1.4vh;
    padding: 1.4vh 2vh;
    border-bottom: 1px solid var(--divider);
    background: var(--surface);
    animation: ak-enter 0.45s ease 0.05s both;
}

.chat-avatar {
    width: 5vh;
    height: 5vh;
    min-width: 36px;
    min-height: 36px;
    border-radius: 50%;
    object-fit: cover;
}

.chat-name {
    font-size: clamp(15px, 2.2vh, 20px);
    font-weight: bold;
    color: var(--text);
}

.chat-box {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

.bubble.bubble-enter {
    animation: ak-bubble-in 0.4s ease both;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1.2vh;
    padding: 2.4vh max(2vh, calc((100% - 900px) / 2));
}

.bubble {
    position: relative; /* anchors the reaction badge */
    max-width: min(82%, 640px);
    padding: 1vh 1.6vh;
    border-radius: 1.4vh;
    font-size: clamp(13px, 1.9vh, 17px);
    line-height: 1.45;
    white-space: pre-wrap;
    word-break: break-word;
}

.bubble-owner {
    align-self: flex-start;
    background: rgba(var(--accent-rgb), 0.12);
    border: 1px solid var(--accent);
    color: var(--text);
    border-bottom-left-radius: 0.3vh;
}

.bubble-guest {
    align-self: flex-end;
    background: var(--bubble-guest-bg);
    border: 1px solid var(--bubble-guest-border);
    color: var(--text);
    border-bottom-right-radius: 0.3vh;
}

.bubble-typing {
    opacity: 0.6;
    font-style: italic;
}

/* Inline image attachment. Constrained to the bubble width and a sane height; the
   intrinsic aspect-ratio (set inline from w/h) reserves the box before load. */
.bubble-img {
    display: block;
    max-width: 100%;
    max-height: 50vh;
    width: auto;
    height: auto;
    border-radius: 0.8vh;
    object-fit: contain;
    background: rgba(127, 127, 127, 0.12); /* placeholder tint while loading */
}

/* Caption under an image (optional). */
.bubble-caption {
    margin-top: 0.6vh;
}

/* The owner's Telegram reaction, shown as a small pill overlapping the bubble's
   squared (tail) corner — bottom-right for guest messages, bottom-left for owner. */
.bubble-reaction {
    position: absolute;
    bottom: -1.2vh;
    padding: 0.1vh 0.7vh;
    border-radius: 1.2vh;
    background: var(--surface);
    border: 1px solid var(--divider);
    box-shadow: 0 1px 6px var(--shadow);
    font-size: clamp(12px, 1.6vh, 15px);
    line-height: 1.4;
    white-space: nowrap;
    z-index: 1;
}

.bubble-guest .bubble-reaction {
    right: 0.6vh;
}

.bubble-owner .bubble-reaction {
    left: 0.6vh;
}

/* Dymek z odznaką dostaje dolny zapas, by pigułka (absolutna, w dolnym rogu) nie
   nachodziła na ostatnią linię tekstu. `em` skaluje się z fontem dymka (dominanta
   wysokości odznaki), `vh` dokłada margines bezpieczeństwa. */
.bubble:has(.bubble-reaction) {
    padding-bottom: calc(1em + 0.2vh);
}

/* Owner bubbles are tappable to leave a quick reaction (relayed to Telegram). */
.bubble-owner[data-mid] {
    cursor: pointer;
}

/* Quick-reaction bar, opened above an owner bubble on tap. */
.reaction-bar {
    position: absolute;
    bottom: 100%;
    left: 0;
    margin-bottom: 0.6vh;
    display: flex;
    gap: 0.2vh;
    padding: 0.4vh;
    background: var(--surface);
    border: 1px solid var(--divider);
    border-radius: 1.4vh;
    box-shadow: 0 0 18px var(--shadow);
    z-index: 3;
}

/* Brak miejsca nad bąbelkiem (np. pierwsza wiadomość przy górnej krawędzi listy) —
   pasek otwiera się pod bąbelkiem, by nie chować się pod .chat-bar / nie był przycięty. */
.reaction-bar.reaction-bar--below {
    top: 100%;
    bottom: auto;
    margin-top: 0.6vh;
    margin-bottom: 0;
}

.reaction-opt {
    background: transparent;
    border: none;
    color: var(--text); /* glify tekstowe (↩) dziedziczą kolor motywu; emoji i tak są kolorowe */
    border-radius: 0.8vh;
    padding: 0.4vh 0.6vh;
    font-size: clamp(16px, 2.4vh, 22px);
    line-height: 1;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.1s ease;
}

.reaction-opt:hover {
    background: rgba(var(--accent-rgb), 0.14);
    transform: scale(1.15);
}

.reaction-opt.active {
    background: rgba(var(--accent-rgb), 0.22);
}

/* Brief cue that a reaction couldn't be delivered (e.g. an old, unmapped message). */
.bubble.reaction-failed {
    animation: reaction-shake 0.4s ease;
    box-shadow: 0 0 0 1px #e0564e;
}

@keyframes reaction-shake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-3px); }
    40%, 80% { transform: translateX(3px); }
}

/* The ↩ reply button in the action bar — separated from the reactions. */
.reaction-opt.action-reply {
    font-size: clamp(15px, 2.2vh, 20px);
}
.reaction-bar .action-reply:not(:first-child) {
    margin-left: 0.2vh;
    padding-left: 0.7vh;
    border-left: 1px solid var(--divider);
    border-radius: 0;
}

/* Quoted message shown inside a bubble (both reply directions). */
.bubble-reply {
    margin-bottom: 0.5vh;
    padding: 0.2vh 0.7vh;
    border-left: 2px solid var(--accent);
    border-radius: 0.4vh;
    background: rgba(var(--accent-rgb), 0.10);
    color: var(--text);
    opacity: 0.75;
    font-size: 0.85em;
    line-height: 1.35;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer; /* klik przewija do cytowanej wiadomości i ją wyróżnia */
}

/* Chwilowe wyróżnienie wiadomości, do której skoczono z cytatu (klik w .bubble-reply).
   Puls tła zamiast zewnętrznego box-shadow — nie pozoruje zmiany rozmiaru bąbelka. */
.bubble.bubble-highlight {
    animation: bubble-highlight 1.3s ease;
}

@keyframes bubble-highlight {
    from { background-color: rgba(var(--accent-rgb), 0.45); }
    /* brak `to` dla background-color → płynny powrót do własnego tła bąbelka
       (owner/guest, motyw jasny/ciemny) */
}

/* "Replying to …" chip above the composer, in normal flow. */
.compose-reply {
    display: flex;
    align-items: center;
    gap: 0.8ch;
    padding: 1vh max(2vh, calc((100% - 900px) / 2));
    border-top: 1px solid var(--divider);
    background: var(--surface);
    font-size: clamp(12px, 1.7vh, 15px);
}

.compose-reply-label {
    color: var(--accent);
    flex-shrink: 0;
}

.compose-reply-text {
    flex: 1;
    min-width: 0;
    color: var(--text);
    opacity: 0.7;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.compose-reply-cancel {
    flex-shrink: 0;
    background: transparent;
    border: none;
    color: var(--text);
    opacity: 0.6;
    cursor: pointer;
    font-size: 1.1em;
    line-height: 1;
    padding: 0.4vh;
}

.compose-reply-cancel:hover {
    opacity: 1;
}

/* ===== Rendered Markdown inside bubbles ===== */
/* Blokowe elementy renderera (nagłówki, listy, cytaty, bloki kodu). Marginesy
   skrojone do bąbelka — pierwszy/ostatni bez zewnętrznego odstępu. */
.bubble h1,
.bubble h2,
.bubble h3,
.bubble h4,
.bubble h5,
.bubble h6 {
    margin: 0.6em 0 0.3em;
    font-size: 1.1em;
    line-height: 1.3;
}

.bubble ul,
.bubble ol {
    margin: 0.4em 0;
    padding-left: 1.6em;
}

.bubble li {
    margin: 0.15em 0;
}

.bubble blockquote {
    margin: 0.4em 0;
    padding: 0.1em 0 0.1em 0.9em;
    border-left: 3px solid var(--accent);
    color: var(--muted);
}

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

.bubble a:hover {
    text-decoration: underline;
}

.bubble code {
    background: var(--code-bg);
    padding: 0.1em 0.35em;
    border-radius: 0.4vh;
    font-size: 0.92em;
}

.bubble pre {
    margin: 0.4em 0;
    padding: 1vh 1.2vh;
    background: var(--code-bg);
    border-radius: 0.8vh;
    overflow-x: auto;
    white-space: pre; /* zachowaj formatowanie kodu; pasek przewijania zamiast zawijania */
}

/* Kod w bloku nie potrzebuje własnego tła/paddingu — dziedziczy z <pre>. */
.bubble pre code {
    background: none;
    padding: 0;
    font-size: 0.92em;
}

/* Zbędny zewnętrzny margines pierwszego/ostatniego bloku w bąbelku. */
.bubble > :first-child {
    margin-top: 0;
}

/* Trymuj dolny margines OSTATNIEGO elementu w przepływie, by treść przylegała do
   paddingu dymka. Pomija absolutne nakładki (.reaction-bar, .bubble-reaction)
   dopinane na końcu — inaczej ich dopięcie przesuwałoby :last-child i przywracało
   wytrymowany margines, obniżając tekst i powiększając dymek. */
.bubble > :not(.reaction-bar):not(.bubble-reaction):not(:has(~ :not(.reaction-bar):not(.bubble-reaction))) {
    margin-bottom: 0;
}

.chat-composer,
.contact-capture {
    display: flex;
    gap: 1vh;
    align-items: center;
    padding: 1.6vh max(2vh, calc((100% - 900px) / 2));
    border-top: 1px solid var(--divider);
    background: var(--surface);
}

.chat-composer {
    position: relative;
    align-items: flex-end; /* keep buttons aligned to the last line as the field grows */
    animation: ak-enter 0.45s ease 0.16s both; /* chrome: wchodzi chwilę po pasku */
}

/* Szanuj preferencję ograniczenia ruchu — szkielet i dymki pojawiają się od razu.
   Umieszczone po wszystkich animowanych regułach i z dopasowaną specyficznością
   (.bubble.bubble-enter), żeby pewnie je nadpisać. */
@media (prefers-reduced-motion: reduce) {
    .chat-bar,
    .chat-composer,
    .bubble.bubble-enter {
        animation: none;
    }
}

.cmd-suggestions {
    position: absolute;
    bottom: 100%;
    left: max(2vh, calc((100% - 900px) / 2));
    right: max(2vh, calc((100% - 900px) / 2));
    margin: 0 0 1vh 0;
    padding: 0.6vh 0;
    list-style: none;
    background: var(--surface);
    border: 1px solid var(--accent);
    border-radius: 1vh;
    box-shadow: 0 0 18px var(--shadow);
    max-height: 56vh;
    overflow-y: auto;
    z-index: 5; /* nad .reaction-bar (z-index: 3), by podpowiedzi komend nie chowały się pod reakcjami */
}

.cmd-suggestion {
    display: flex;
    align-items: baseline;
    gap: 1.2ch;
    padding: 1vh 1.6vh;
    cursor: pointer;
}

.cmd-suggestion.active,
.cmd-suggestion:hover {
    background: rgba(var(--accent-rgb), 0.14);
}

.cmd-name {
    color: var(--accent);
    font-weight: bold;
    white-space: nowrap;
}

.cmd-desc {
    color: var(--muted);
    font-size: clamp(12px, 1.7vh, 15px);
}

.chat-input {
    flex: 1;
    min-width: 0;
    min-height: var(--control-h); /* podłoga równa przyciskom; autoGrow rośnie powyżej */
    background: var(--input-bg);
    border: 1px solid var(--input-border);
    border-radius: 1vh;
    color: var(--text);
    font-family: 'Fira Code', Font, monospace;
    font-size: clamp(13px, 1.9vh, 16px);
    padding: 1.2vh 1.4vh;
    outline: none;
}

.chat-input:focus {
    border-color: var(--accent);
}

/* Multi-line message field: grows with content (height set by autoGrowInput in
   chat.js) up to a cap, then scrolls. Scoped to the textarea so the single-line
   #contact-input (an <input> sharing .chat-input) is unaffected. */
textarea.chat-input {
    resize: none;
    overflow-y: hidden; /* JS flips to auto past the row cap */
    line-height: 1.4;
}

.chat-send {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: var(--control-h); /* zrównane z polem tekstowym (jedna linijka) */
    background: var(--accent);
    color: var(--on-accent);
    border: none;
    border-radius: 1vh;
    padding: 0 2vh;
    font-family: 'Fira Code', Font, monospace;
    font-weight: bold;
    white-space: nowrap;
    cursor: pointer;
    transition: background 0.2s ease;
}

.chat-send:hover {
    background: var(--accent-hover);
}

.chat-send:focus-visible {
    outline: 2px solid var(--focus-ring);
    outline-offset: 2px;
}

/* Attach (📎) — available on all devices (unlike the emoji button). */
.attach-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: var(--control-h);
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--input-border);
    border-radius: 1vh;
    padding: 0 1.4vh;
    font-size: clamp(15px, 2vh, 18px);
    line-height: 1;
    cursor: pointer;
    transition: border-color 0.2s ease, background 0.2s ease;
}

.attach-toggle:hover {
    border-color: var(--accent);
}

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

/* ===== Emoji picker (desktop only) ===== */
/* Hidden by default; only a device with a real pointer (desktop) reveals it —
   phones already surface emoji on the native keyboard. */
.emoji-toggle {
    display: none;
    height: var(--control-h); /* zrównane z polem tekstowym (jedna linijka) */
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--input-border);
    border-radius: 1vh;
    padding: 0 1.4vh;
    font-size: clamp(15px, 2vh, 18px);
    line-height: 1;
    cursor: pointer;
    transition: border-color 0.2s ease, background 0.2s ease;
}

.emoji-toggle:hover {
    border-color: var(--accent);
}

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

@media (hover: hover) and (pointer: fine) {
    .emoji-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
}

/* Anchored like .cmd-suggestions (full composer width) so the grid is roomy.
   Lays out as a column: search field, category tabs, then the scrolling body. */
.emoji-panel {
    position: absolute;
    bottom: 100%;
    left: max(2vh, calc((100% - 900px) / 2));
    right: max(2vh, calc((100% - 900px) / 2));
    margin: 0 0 1vh 0;
    padding: 0.8vh;
    display: flex;
    flex-direction: column;
    gap: 0.6vh;
    background: var(--surface);
    border: 1px solid var(--divider);
    border-radius: 1vh;
    box-shadow: 0 0 18px var(--shadow);
}

/* Search field + skin-tone selector share one row. */
.emoji-searchrow {
    display: flex;
    align-items: center;
    gap: 0.6vh;
}

.emoji-search {
    flex: 1;
    min-width: 0;
    box-sizing: border-box;
    background: var(--input-bg);
    color: var(--text);
    border: 1px solid var(--input-border);
    border-radius: 0.8vh;
    padding: 0.8vh 1.2vh;
    font: inherit;
    font-size: clamp(13px, 1.8vh, 16px);
}

.emoji-search:focus-visible {
    outline: none;
    border-color: var(--accent);
}

/* The "one place" to pick the global skin tone: default (✋) + five tones. */
.emoji-tone {
    display: flex;
    gap: 0.2vh;
    flex-shrink: 0;
}

.emoji-swatch {
    background: transparent;
    border: 1px solid transparent;
    border-radius: 0.6vh;
    padding: 0.4vh;
    font-size: clamp(15px, 2.2vh, 20px);
    line-height: 1;
    cursor: pointer;
    opacity: 0.7;
    transition: opacity 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.emoji-swatch:hover {
    opacity: 1;
    background: rgba(var(--accent-rgb), 0.14);
}

.emoji-swatch.active {
    opacity: 1;
    border-color: var(--accent);
}

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

.emoji-tabs {
    display: flex;
    justify-content: space-between;
    gap: 0.2vh;
    padding-bottom: 0.4vh;
    border-bottom: 1px solid var(--divider);
}

.emoji-tab {
    flex: 1;
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    border-radius: 0.6vh 0.6vh 0 0;
    padding: 0.6vh 0;
    font-size: clamp(15px, 2.2vh, 20px);
    line-height: 1;
    cursor: pointer;
    opacity: 0.6;
    transition: opacity 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.emoji-tab:hover {
    opacity: 1;
    background: rgba(var(--accent-rgb), 0.14);
}

.emoji-tab.active {
    opacity: 1;
    border-bottom-color: var(--accent);
}

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

.emoji-body {
    max-height: 44vh;
    overflow-y: auto;
}

.emoji-section-title {
    position: sticky;
    top: 0;
    background: var(--surface);
    color: var(--text);
    opacity: 0.55;
    font-size: clamp(11px, 1.5vh, 13px);
    padding: 0.6vh 0.2vh 0.4vh;
}

.emoji-grid,
.emoji-results {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 0.2vh;
}

.emoji-results {
    max-height: 44vh;
    overflow-y: auto;
}

.emoji-empty {
    color: var(--text);
    opacity: 0.55;
    text-align: center;
    padding: 2vh 0;
    font-size: clamp(13px, 1.8vh, 16px);
}

.emoji-item {
    background: transparent;
    border: none;
    border-radius: 0.8vh;
    padding: 0.6vh;
    font-size: clamp(18px, 2.6vh, 24px);
    line-height: 1;
    cursor: pointer;
}

.emoji-item:hover,
.emoji-item:focus-visible {
    background: rgba(var(--accent-rgb), 0.14);
    outline: none;
}

/* honeypot: off-screen, real users never see or fill it */
.hp-field {
    position: absolute;
    left: -9999px;
    width: 1px;
    height: 1px;
    opacity: 0;
}
