/* ── Reset & Variables ──────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg:        #0f1117;
    --surface:   #1a1d27;
    --surface2:  #22263a;
    --border:    #2e3347;
    --text:      #e8eaf0;
    --muted:     #8890a8;
    --accent:    #4ade80;
    --accent2:   #60a5fa;
    --danger:    #f87171;
    --radius:    10px;
    --font:      'Segoe UI', system-ui, sans-serif;
    --panel-w:   320px;
}

html, body {
    height: 100%;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.5;
}

/* ── Utilities ────────────────────────────────────────────────────────────── */
.hidden { display: none !important; }
.error { color: var(--danger); font-size: .875rem; margin-top: .75rem; }

/* ── Center layout (home + join) ─────────────────────────────────────────── */
.center-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
    background: radial-gradient(ellipse at 60% 20%, #1a2a1a 0%, var(--bg) 70%);
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 2.5rem 2rem;
    width: 100%;
    max-width: 420px;
    box-shadow: 0 8px 40px rgba(0,0,0,.4);
}

.logo { font-size: 1.6rem; font-weight: 700; margin-bottom: .5rem; letter-spacing: -.5px; }
.tagline { color: var(--muted); margin-bottom: 1.75rem; font-size: .95rem; }
.card h2 { font-size: 1.25rem; font-weight: 600; margin-bottom: .35rem; }
.card h2 em { color: var(--accent); font-style: normal; }

/* ── Forms ────────────────────────────────────────────────────────────────── */
label {
    display: block;
    font-size: .8rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .06em;
    margin-bottom: .4rem;
}

input[type="text"] {
    display: block;
    width: 100%;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    padding: .75rem 1rem;
    font-size: 1rem;
    font-family: inherit;
    outline: none;
    transition: border-color .15s;
}

input[type="text"]:focus { border-color: var(--accent); }
input[readonly] { color: var(--muted); font-size: .85rem; cursor: text; }

/* Primary button — scoped to .card forms only, not map chat form */
.card button[type="submit"],
.card form button,
.card > a.btn-primary {
    display: block;
    width: 100%;
    margin-top: 1rem;
    padding: .85rem 1rem;
    background: var(--accent);
    color: #0a0f0a;
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    transition: opacity .15s, transform .1s;
    font-family: inherit;
}

button[type="submit"]:hover { opacity: .9; }
button[type="submit"]:active { transform: scale(.98); }
button:disabled { opacity: .5; cursor: not-allowed; }

/* ── Invite box (home page) ───────────────────────────────────────────────── */
.invite-box {
    margin-top: 1.5rem;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
}

.invite-box p { font-size: .875rem; color: var(--muted); margin-bottom: .75rem; }

.invite-link-row { display: flex; gap: .5rem; }
.invite-link-row input { flex: 1; }
.invite-link-row button {
    padding: .75rem 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    font-size: .875rem;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    margin-top: 0;
    width: auto;
    transition: background .15s;
}

.invite-link-row button:hover { background: var(--border); }

.btn-secondary {
    display: block;
    text-align: center;
    margin-top: 1rem;
    padding: .75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--accent2);
    text-decoration: none;
    font-size: .95rem;
    font-weight: 600;
    transition: background .15s;
}

.btn-secondary:hover { background: var(--surface2); }

/* ── Map page top bar ─────────────────────────────────────────────────────── */
body:has(#map) {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
}

#top-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .6rem 1rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    z-index: 100;
}

.group-title { font-weight: 700; font-size: 1rem; letter-spacing: -.3px; }

.top-actions { display: flex; gap: .4rem; align-items: center; }

.tb-btn {
    padding: .35rem .55rem;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 7px;
    color: var(--accent2);
    font-size: 1rem;        /* emoji renders at readable size */
    line-height: 1;
    cursor: pointer;
    margin-top: 0;
    width: auto;
    transition: background .15s, color .15s, border-color .15s;
}

.tb-btn:hover { background: var(--border); }
.tb-btn--off    { color: var(--muted); border-color: transparent; background: transparent; }
.tb-btn--off:hover { background: var(--surface2); color: var(--text); }
.tb-btn--danger { color: var(--danger); }
.tb-btn--danger:hover { background: rgba(248,113,113,.12); border-color: var(--danger); }

#invite-banner {
    display: flex;
    gap: .5rem;
    padding: .6rem 1rem;
    background: var(--surface2);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#invite-banner input { flex: 1; padding: .5rem .75rem; font-size: .875rem; }

#invite-banner button {
    padding: .5rem 1rem;
    background: var(--accent);
    color: #0a0f0a;
    border: none;
    border-radius: var(--radius);
    font-weight: 700;
    cursor: pointer;
    font-size: .875rem;
    white-space: nowrap;
    margin-top: 0;
    width: auto;
}

/* ── Main layout ──────────────────────────────────────────────────────────── */
#app { display: flex; flex: 1; overflow: hidden; min-height: 0; }

#map-wrap { flex: 1; position: relative; min-width: 0; }
#map { width: 100%; height: 100%; }

#panel {
    width: var(--panel-w);
    background: var(--surface);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;       /* hard ceiling — children cannot overflow this */
    flex-shrink: 0;
    min-height: 0;
    transition: width .25s ease;
}

/* ── Members section ──────────────────────────────────────────────────────── */
#members-section {
    padding: .75rem 1rem .5rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;         /* never steals space from chat */
    overflow: hidden;
}

.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    padding: .1rem 0 .5rem;
    user-select: none;
}

.section-header:hover h3 { color: var(--text); }

.chevron {
    font-size: .75rem;
    color: var(--muted);
    transition: transform .2s;
}

#members-section h3, #chat-section h3 {
    font-size: .8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .07em;
    color: var(--muted);
    margin-bottom: 0;
    transition: color .15s;
}

/* Collapsed states */
#member-list.collapsed { display: none; }
#chat-body.collapsed   { display: none; }

#member-list {
    list-style: none;
    max-height: 160px;      /* hard cap so it never squeezes chat */
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.member-row {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .4rem .5rem;
    border-radius: 7px;
    transition: background .12s;
}

.member-row:hover { background: var(--surface2); }
.member-row.is-self { background: rgba(74,222,128,.06); }

.dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.dot.online  { background: var(--accent); box-shadow: 0 0 6px var(--accent); }
.dot.offline { background: var(--muted); }

.mname {
    flex: 1;
    font-size: .9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mtime  { font-size: .75rem; color: var(--muted); flex-shrink: 0; }
.mdist  { font-size: .75rem; color: var(--accent2); flex-shrink: 0; font-variant-numeric: tabular-nums; }

/* ── Chat section ─────────────────────────────────────────────────────────── */
#chat-section {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;          /* crucial — allows flex child to shrink below content size */
    overflow: hidden;
    padding: .75rem 1rem .75rem;
}

#chat-body {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;          /* same — without this the form gets pushed out */
    overflow: hidden;
}

#chat-messages {
    flex: 1;
    min-height: 0;          /* scrollable area shrinks, never overflows */
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: .4rem;
    padding-right: 2px;
    padding-bottom: 4px;
}

.chat-msg {
    background: var(--surface2);
    border-radius: 8px;
    padding: .5rem .75rem;
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    gap: 0 .5rem;
    transition: background .12s;
}

.chat-msg:hover { background: var(--border); }
.chat-msg.is-self { background: rgba(74,222,128,.1); }
.chat-name { font-size: .72rem; font-weight: 700; color: var(--accent2); grid-column: 1; grid-row: 1; }
.chat-msg.is-self .chat-name { color: var(--accent); }
.chat-time { font-size: .7rem; color: var(--muted); grid-column: 2; grid-row: 1; align-self: center; }
.chat-body { font-size: .9rem; color: var(--text); grid-column: 1 / -1; grid-row: 2; word-break: break-word; }

#chat-form {
    display: flex;
    gap: .5rem;
    padding: .6rem 1rem;
    background: var(--surface);
    border-top: 1px solid var(--border);
    flex-shrink: 0;  /* sits naturally at bottom of panel on desktop */
}

/* Mobile only: form is moved to <body> by JS so no ancestor clips it */
@media (max-width: 680px) {
    /* Space so last message isn't hidden behind the floated form */
    #chat-messages { padding-bottom: 64px; }
}

/* Floated chat form — appended to <body> on mobile by JS */
.chat-form-floated {
    position: fixed !important;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 2000;
    background: var(--surface) !important;
    border-top: 1px solid var(--border);
    padding: .6rem .75rem !important;
    padding-bottom: calc(.6rem + env(safe-area-inset-bottom, 0px)) !important;
    box-shadow: 0 -3px 16px rgba(0,0,0,.5);
    margin-top: 0 !important;
    display: flex;
    gap: .5rem;
}

#chat-form input { flex: 1; padding: .6rem .85rem; font-size: .9rem; }

#chat-form button {
    padding: .6rem 1rem;
    background: var(--accent);
    color: #0a0f0a;
    border: none;
    border-radius: var(--radius);
    font-weight: 700;
    cursor: pointer;
    font-size: .875rem;
    white-space: nowrap;
    margin-top: 0;
    width: auto;
    transition: opacity .15s;
}

#chat-form button:hover { opacity: .9; }

/* ── Map markers ──────────────────────────────────────────────────────────── */
.map-pin {
    width: 36px;
    height: 36px;
    border-radius: 50% 50% 50% 0;
    transform: rotate(-45deg);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 13px;
    color: #0a0f0a;
    box-shadow: 0 2px 8px rgba(0,0,0,.4);
}

.map-pin-inner {
    transform: rotate(45deg);
    line-height: 1;
}

.msg-pin { font-size: 22px; filter: drop-shadow(0 2px 4px rgba(0,0,0,.5)); }

/* ── Panel hidden → map expands to full width ────────────────────────────── */
#panel.panel--hidden {
    width: 0 !important;
    border-left: none !important;
    min-width: 0;
    overflow: hidden;
}

/* Fullscreen: ensure full coverage and fix chat form above keyboard */
:fullscreen #app,
:-webkit-full-screen #app {
    height: 100dvh;
}

/* In fullscreen, fix form to bottom just like mobile */
body.is-fullscreen #chat-form {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    box-shadow: 0 -2px 12px rgba(0,0,0,.3);
}

/* ── Scrollbars ───────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Mobile ───────────────────────────────────────────────────────────────── */
@media (max-width: 680px) {
    :root { --panel-w: 100%; }

    /* Stack vertically */
    #app { flex-direction: column; }

    /* Map: takes all remaining space — grows when panel shrinks/hides */
    #map-wrap { flex: 1; min-height: 150px; }

    /* Panel: fixed height chunk below map */
    #panel {
        width: 100%;
        flex: none;
        height: 50vh;
        min-height: 0;
        border-left: none;
        border-top: 1px solid var(--border);
        transition: height .25s ease;
        overflow: hidden;
    }

    /* Hidden on mobile = zero height → map fills full screen */
    #panel.panel--hidden {
        height: 0 !important;
        border-top: none;
        overflow: hidden;
    }

    #member-list { max-height: 80px; }


}
