/* Clinic dashboard styling.
 *
 * Hand-written, no framework. The patient chat pulls Tailwind from a CDN at page
 * load; this page does not, so its content security policy can forbid every
 * external origin — which matters more here than there, because this is the page
 * that renders text written by strangers to someone who is logged in.
 *
 * Layout is WhatsApp's: a list beside a conversation on a wide screen, two full
 * screens with a back arrow on a narrow one. The clinic owner will read this on
 * a phone between patients.
 */

:root {
    --bg:              #f0f2f5;
    --surface:         #ffffff;
    --surface-sunken:  #f7f8fa;
    --border:          #e3e6ea;
    --text:            #111b21;
    --text-muted:      #667781;
    --accent:          #128c7e;
    --accent-dark:     #0d6d62;
    --bubble-patient:  #ffffff;
    --bubble-bot:      #d9fdd3;
    --danger:          #c0392b;
    --unread:          #25d366;

    --radius:   12px;
    --header-h: 64px;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                 "Helvetica Neue", Arial, sans-serif;
    font-size: 15px;
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;
}

.hidden { display: none !important; }

.screen { height: 100%; }

/* This is an app shell: the viewport is the frame, and only the list and the
 * transcript scroll. `dvh` rather than the `%` above because a phone browser's
 * URL bar is part of the viewport height until you scroll — measured against
 * `100%` the read-only footer and the newest message sit underneath it. Written
 * as an override so a browser without `dvh` keeps the `%` and loses nothing.
 * It must stay *after* the two rules it overrides: same specificity, later wins. */
@supports (height: 100dvh) {
    html, body, .screen { height: 100dvh; }
}

/* ------------------------------------------------------------------ Login */

#login-screen {
    display: flex;
    justify-content: center;
    padding: 24px;
    /* Scrolls, because on a phone the on-screen keyboard can leave less room
     * than the card needs. Centring is `margin: auto` on the card rather than
     * `align-items: center` here: a centred flex item that overflows its
     * container has its top edge pushed out of reach, and the top is where the
     * email field is. */
    overflow-y: auto;
}

.login-card {
    margin: auto;
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: 0 2px 16px rgba(0, 0, 0, .08);
    padding: 32px 28px;
    width: 100%;
    max-width: 380px;
}

.login-title { margin: 0 0 6px; font-size: 22px; }
.login-sub   { margin: 0 0 24px; color: var(--text-muted); font-size: 14px; }

.field { display: block; margin-bottom: 16px; }
.field > span {
    display: block;
    margin-bottom: 6px;
    font-size: 13px;
    color: var(--text-muted);
}
.field input {
    width: 100%;
    padding: 11px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 16px; /* 16px stops iOS zooming the page on focus */
    background: var(--surface-sunken);
}
.field input:focus {
    outline: 2px solid var(--accent);
    outline-offset: -1px;
    background: var(--surface);
}

.btn-primary {
    width: 100%;
    padding: 12px;
    border: 0;
    border-radius: 8px;
    background: var(--accent);
    color: #fff;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-dark); }
.btn-primary:disabled { opacity: .6; cursor: default; }

.login-error {
    margin: 0 0 14px;
    padding: 10px 12px;
    border-radius: 8px;
    background: #fdecea;
    color: var(--danger);
    font-size: 14px;
}
.login-help {
    margin: 16px 0 0;
    text-align: center;
    font-size: 12px;
    color: var(--text-muted);
}

/* -------------------------------------------------------------- App shell */

#app-screen {
    display: grid;
    grid-template-columns: minmax(300px, 380px) 1fr;
    height: 100%;
}

.pane {
    height: 100%;
    min-height: 0;
    /* Load-bearing, and the whole reason the phone view was cut off until
     * 2026-08-13. A grid item's default minimum width is `auto`, meaning its
     * min-content width — and `.row-preview` is `white-space: nowrap`, whose
     * min-content width is the entire message. `overflow: hidden` does not
     * reduce it: it clips painting, not intrinsic size. So the list pane's
     * minimum became "as wide as the longest thing the bot ever said", the
     * `1fr` phone track had no explicit minimum to stop it, and the page grew
     * past the screen. Being RTL, the browser parks the overflow against the
     * right edge, so the part that vanished was the left. The desktop track is
     * `minmax(300px, 380px)` — an explicit minimum — which is why only phones
     * showed it. */
    min-width: 0;
    display: flex;
    flex-direction: column;
    background: var(--surface);
}

.list-pane { border-inline-end: 1px solid var(--border); }
.chat-pane { background: var(--surface-sunken); }

.pane-header {
    flex: 0 0 auto;
    min-height: var(--header-h);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.clinic { display: flex; align-items: center; gap: 10px; min-width: 0; }
/* The text block beside the logo. Same `min-width: 0` as everywhere else in this
 * file: an email address has no spaces, so it cannot wrap, so its min-content
 * width is the whole address — enough to push the header wider than a small
 * phone. */
.clinic > div { min-width: 0; }
.clinic-logo {
    flex: 0 0 auto;
    width: 40px; height: 40px;
    border-radius: 50%;
    object-fit: cover;
    background: var(--surface-sunken);
}
.clinic-name  {
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.clinic-email {
    font-size: 12px;
    color: var(--text-muted);
    direction: ltr;
    text-align: start;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-quiet {
    border: 0;
    background: none;
    color: var(--text-muted);
    font-size: 14px;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
}
.btn-quiet:hover { background: var(--surface-sunken); color: var(--text); }

.search-row { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.search-row input {
    width: 100%;
    padding: 9px 12px;
    border: 0;
    border-radius: 999px;
    background: var(--surface-sunken);
    font-size: 14px;
}
.search-row input:focus { outline: 2px solid var(--accent); }

/* --------------------------------------------------------- Session list */

.session-list { flex: 1 1 auto; overflow-y: auto; }

.session-row {
    display: flex;
    gap: 12px;
    align-items: center;
    width: 100%;
    padding: 12px 16px;
    border: 0;
    border-bottom: 1px solid var(--border);
    background: none;
    text-align: start;
    cursor: pointer;
    font: inherit;
    color: inherit;
}
.session-row:hover    { background: var(--surface-sunken); }
.session-row.selected { background: #e9f5f3; }

.avatar {
    flex: 0 0 auto;
    width: 46px; height: 46px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    font-weight: 600;
}

.row-body { flex: 1 1 auto; min-width: 0; }
.row-top {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
}
.row-name {
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    /* A flex item, so it needs its own `min-width: 0` — without it a long
     * patient name shoves the timestamp off the row instead of truncating. */
    min-width: 0;
}
.row-time { flex: 0 0 auto; font-size: 12px; color: var(--text-muted); }

.row-preview {
    color: var(--text-muted);
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Wraps: a session can carry a photo count, a booking pill and a phone number
 * at once, which does not fit one line on a narrow phone. */
.row-tags {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    margin-top: 4px;
}

.tag {
    font-size: 11px;
    padding: 1px 7px;
    border-radius: 999px;
    background: var(--surface-sunken);
    color: var(--text-muted);
    border: 1px solid var(--border);
}
.tag.booked { background: #e7f7ec; color: #0d6d3d; border-color: #bfe6cd; }

.unread-dot {
    flex: 0 0 auto;
    width: 10px; height: 10px;
    border-radius: 50%;
    background: var(--unread);
}

.empty {
    padding: 32px 20px;
    text-align: center;
    color: var(--text-muted);
    font-size: 14px;
}
.empty.error { color: var(--danger); }

/* --------------------------------------------------------- Conversation */

.placeholder {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
}

.chat-view {
    height: 100%;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.chat-header { gap: 8px; }
.back-btn { display: none; font-size: 20px; line-height: 1; }
.chat-who { flex: 1 1 auto; min-width: 0; }
.chat-name {
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.chat-phone {
    font-size: 13px;
    color: var(--accent);
    text-decoration: none;
    display: inline-block;
}
.chat-badges { display: flex; gap: 6px; flex: 0 0 auto; }

.messages {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 20px 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.msg {
    max-width: min(72%, 560px);
    padding: 8px 12px;
    border-radius: var(--radius);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    box-shadow: 0 1px 1px rgba(0, 0, 0, .06);
}
/* Patient on the right, clinic's bot on the left — matching what the patient
 * sees in her own chat, so the clinic reads the same shape her patient did. */
.msg.patient {
    align-self: flex-start;
    background: var(--bubble-patient);
    border-start-start-radius: 2px;
}
.msg.assistant {
    align-self: flex-end;
    background: var(--bubble-bot);
    border-start-end-radius: 2px;
}
.msg a { color: var(--accent-dark); }

.msg-photo-note {
    display: block;
    margin-top: 4px;
    font-size: 12px;
    color: var(--text-muted);
}

/* --------------------------------------------------------------- Photos */

.photo-strip {
    flex: 0 0 auto;
    padding: 12px 16px;
    background: var(--surface);
    border-top: 1px solid var(--border);
}
.photo-strip-title { font-size: 13px; color: var(--text-muted); margin-bottom: 8px; }
.photo-thumbs { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; }
.photo-thumb {
    flex: 0 0 auto;
    width: 84px; height: 84px;
    border: 1px solid var(--border);
    border-radius: 8px;
    object-fit: cover;
    cursor: zoom-in;
    background: var(--surface-sunken);
}
.photo-note { margin: 8px 0 0; font-size: 11px; color: var(--text-muted); }

.read-only-note {
    flex: 0 0 auto;
    padding: 12px 16px;
    background: var(--surface);
    border-top: 1px solid var(--border);
    font-size: 12px;
    color: var(--text-muted);
    text-align: center;
}

.lightbox {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .85);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    z-index: 50;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }
.lightbox-close {
    position: absolute;
    top: 16px; inset-inline-end: 16px;
    width: 40px; height: 40px;
    border: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, .15);
    color: #fff;
    font-size: 18px;
    cursor: pointer;
}

/* --------------------------------------------------------------- Phones */

@media (max-width: 767px) {
    /* `minmax(0, 1fr)`, never a bare `1fr` — see the note on `.pane`. The
     * `min-width` there already fixes it; this says the same thing at the track
     * level so removing one does not silently restore the bug. */
    #app-screen {
        grid-template-columns: minmax(0, 1fr);
        /* `viewport-fit=cover` in the meta tag lets the page use the full
         * screen, which means it also reaches under a notch. Padding the shell
         * once keeps every row inside the safe area without repeating this on
         * each of them. Physical left/right on purpose: the `env()` values are
         * physical, and this page is RTL. */
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }

    /* One pane at a time. `.showing-chat` on the shell swaps which. */
    .chat-pane { display: none; }
    #app-screen.showing-chat .list-pane { display: none; }
    #app-screen.showing-chat .chat-pane { display: flex; }

    .back-btn { display: block; }
    .msg { max-width: 85%; }

    /* 16px is the threshold below which iOS Safari zooms the whole page in when
     * a field takes focus — and having zoomed, it does not zoom back out, so
     * the clinic is left dragging a too-wide page sideways. The login fields
     * are already 16px for this reason; the search box was not. */
    .search-row input { font-size: 16px; }

    /* Thumb-sized targets. A 14px label with 8px of padding is around 30px
     * high, under every guideline and genuinely hard to hit one-handed. */
    .btn-quiet {
        min-width: 44px;
        min-height: 44px;
        padding: 10px 12px;
    }
    .back-btn { font-size: 24px; padding: 8px 12px; }

    .messages { padding: 12px 10px; }
    .session-row { padding: 12px 12px; }
    .pane-header { padding: 8px 12px; }

    /* The home-indicator bar overlaps the bottom of the screen. Everything that
     * can sit against it gets cleared. */
    .read-only-note { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
    .session-list   { padding-bottom: env(safe-area-inset-bottom); }
    .lightbox       { padding: max(16px, env(safe-area-inset-top))
                               max(16px, env(safe-area-inset-right))
                               max(16px, env(safe-area-inset-bottom))
                               max(16px, env(safe-area-inset-left)); }
    .lightbox-close { top: max(16px, env(safe-area-inset-top)); }
}
