:root {
    color-scheme: light dark;
    --bg:        #f4f5fb;
    --surface:   #ffffff;
    --surface-2: #eef0f8;
    --text:      #1a1c2c;
    --muted:     #6b7088;
    --border:    #d9dceb;
    --accent:    #5b6ee1;
    --accent-2:  #4754c4;
    --accent-fg: #ffffff;
    --visitor:   #eef1ff;
    --admin:     #e9f7ee;
    --error:     #c0382b;
    --shadow:    0 6px 24px rgba(20, 25, 60, 0.08);
    --radius:    10px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg:        #0f1117;
        --surface:   #181a23;
        --surface-2: #21232f;
        --text:      #e6e8ef;
        --muted:     #8e92a8;
        --border:    #2c2f3d;
        --accent:    #7d8df0;
        --accent-2:  #6473e2;
        --accent-fg: #0f1117;
        --visitor:   #1f2335;
        --admin:     #1a2a23;
        --error:     #ff7a6e;
        --shadow:    0 6px 24px rgba(0, 0, 0, 0.45);
    }
}

* { box-sizing: border-box; }

/* Element-level [hidden] is otherwise overridden by any rule that sets
   display (e.g. our #ticket { display: flex }). */
[hidden] { display: none !important; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    line-height: 1.5;
}

html { height: 100%; }
body {
    /* Body grows to include footer below the fold — page scroll reveals it. */
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

main {
    /* Pin main to one viewport — footer sits just below the fold, requires
       a small page scroll. The chat inside still scrolls inside its own
       container thanks to the min-height:0 flex chain. */
    height: 100vh;
    height: 100dvh;
    width: 100%;
    max-width: 640px;
    margin: 0 auto;
    padding: 2rem 1.25rem;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

h1 {
    margin: 0 0 1.5rem;
    font-size: 1.6rem;
    font-weight: 600;
    letter-spacing: -0.01em;
}

/* --- Welcome block (admin-controlled HTML over the form) --- */

.welcome {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.1rem 1.4rem;
    box-shadow: var(--shadow);
    margin-bottom: 1.25rem;
    color: var(--text);
}
.welcome h1, .welcome h2, .welcome h3 {
    margin: 0 0 0.4rem;
    font-size: 1.1rem;
    font-weight: 600;
}
.welcome p { margin: 0 0 0.6rem; }
.welcome p:last-child { margin-bottom: 0; }
.welcome a { color: var(--accent); text-decoration: none; }
.welcome a:hover { text-decoration: underline; }
.welcome ul, .welcome ol { margin: 0.4rem 0; padding-left: 1.4rem; }

/* --- Form --- */

#form, .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
}

.field {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin-bottom: 1.1rem;
    position: relative;
}

.field .label {
    font-size: 0.875rem;
    color: var(--muted);
    font-weight: 500;
}

input, textarea, select {
    font: inherit;
    color: inherit;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.6rem 0.75rem;
    width: 100%;
    transition: border-color 120ms ease, box-shadow 120ms ease, background 120ms ease;
}

textarea {
    resize: vertical;
    min-height: 6em;
    font-family: inherit;
}

input:focus, textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in oklab, var(--accent) 20%, transparent);
    background: var(--surface);
}

.counter {
    position: absolute;
    right: 0.5rem;
    bottom: 0.4rem;
    font-size: 0.75rem;
    color: var(--muted);
    pointer-events: none;
    background: var(--surface-2);
    padding: 0 0.25rem;
    border-radius: 4px;
}

/* honeypot — offset OFF screen, NOT display:none */
.hp {
    position: absolute;
    left: -10000px;
    width: 1px;
    height: 1px;
    overflow: hidden;
}

/* --- Buttons --- */

.btn {
    font: inherit;
    padding: 0.6rem 1.1rem;
    border-radius: 6px;
    border: 1px solid transparent;
    cursor: pointer;
    transition: background 120ms ease, color 120ms ease, transform 80ms ease;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.55; cursor: progress; }

.btn.primary {
    background: var(--accent);
    color: var(--accent-fg);
    font-weight: 600;
}
.btn.primary:hover:not(:disabled) { background: var(--accent-2); }

.btn.ghost {
    background: transparent;
    color: var(--muted);
    border-color: var(--border);
}
.btn.ghost:hover { color: var(--text); border-color: var(--accent); }

.actions {
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
}

.status {
    margin: 0;
    min-height: 1.4em;
    font-size: 0.9rem;
    color: var(--muted);
}
.status.error { color: var(--error); }

/* --- Ticket view --- */

/* The ticket card grows to fill remaining vertical space, so the chat
   scrolls inside it and the footer stays anchored at the page bottom. */
#ticket {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.ticket-head {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.95rem;
    flex-shrink: 0;
}
.ticket-head code {
    background: var(--surface-2);
    padding: 0.1rem 0.45rem;
    border-radius: 4px;
    font-size: 0.95em;
    letter-spacing: 0.04em;
}
.badge {
    margin-left: auto;
    font-size: 0.75rem;
    padding: 0.2rem 0.55rem;
    border-radius: 999px;
    background: var(--surface-2);
    color: var(--muted);
}
.badge.awaiting_admin   { background: color-mix(in oklab, var(--accent) 15%, transparent); color: var(--accent); }
.badge.awaiting_visitor { background: color-mix(in oklab, #2eb67d 18%, transparent); color: #2eb67d; }
.badge.closed           { background: color-mix(in oklab, var(--error) 15%, transparent); color: var(--error); }

.messages {
    list-style: none;
    margin: 0 0 1rem;
    padding: 0 0.25rem 0 0;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    /* This is the chat scroll container — fills remaining card height,
       header/reply form stay pinned. */
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    min-height: 4rem;
}
.messages li {
    padding: 0.55rem 0.85rem 0.4rem;
    border-radius: 8px;
    word-wrap: break-word;
    max-width: 90%;
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}
.msg-body {
    white-space: pre-wrap;
}
.msg-time {
    font-size: 0.7rem;
    color: var(--muted);
    align-self: flex-end;
    line-height: 1;
}
.messages li.visitor {
    background: var(--visitor);
    align-self: flex-end;
}
.messages li.admin {
    background: var(--admin);
    align-self: flex-start;
}

#reply-form { margin-top: 1rem; flex-shrink: 0; }

.new-ticket { margin-top: 0.75rem; flex-shrink: 0; align-self: flex-start; }

/* --- Footer / language switcher --- */

footer {
    padding: 0.9rem 1.25rem;
    text-align: center;
    color: var(--muted);
    font-size: 0.85rem;
    border-top: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
}
.lang-switch {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}
.legal {
    margin: 0;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.78rem;
    color: var(--muted);
}
.legal a { color: inherit; text-decoration: none; }
.legal a:hover { color: var(--accent); }
.legal .sep { color: var(--border); }
.lang-btn {
    background: none;
    border: none;
    color: var(--muted);
    font: inherit;
    font-size: 0.85rem;
    cursor: pointer;
    padding: 0.2rem 0.4rem;
    border-radius: 4px;
    transition: color 120ms ease, background 120ms ease;
}
.lang-btn:hover { color: var(--text); }
.lang-btn.current {
    color: var(--accent);
    font-weight: 600;
    cursor: default;
}
.lang-sep { color: var(--border); }
