/* ==========================================================================
   Intizar Mart — AI shopping assistant widget

   LAYERING
   The site already has a stack, and this widget must sit UNDER all of it so it
   can never cover something the customer was already using:

       .search-overlay        10000
       .mobile-menu-drawer     9999
       .toast-notification     9999
       .mobile-menu-overlay    9998
       .whatsapp-float         1000   (bottom-LEFT)
       .cart-drawer             999
       .cart-overlay            998
       --- everything above stays on top of us ---
       .ia-panel                950
       .ia-fab                  900

   The WhatsApp button is bottom-left, so bottom-right is free and there is no
   clash. Nothing here restyles an existing selector; every rule is namespaced
   with .ia- so it cannot leak into the storefront.

   Only the site's own custom properties are used, so the widget follows any
   future theme change automatically.
   ========================================================================== */

/* ---------------------------------------------------------------- button -- */

.ia-fab {
    position: fixed;
    bottom: 30px;
    right: 30px;
    z-index: 900;
    width: 60px;
    height: 60px;
    border: none;
    border-radius: 50%;
    background: var(--primary-color);
    color: var(--white);
    font-size: 1.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.2);
    transition: transform var(--transition-speed) ease,
                background-color var(--transition-speed) ease;
}

.ia-fab:hover { transform: scale(1.08); }
.ia-fab:focus-visible { outline: 3px solid var(--secondary-color); outline-offset: 2px; }
.ia-fab[hidden] { display: none; }

.ia-fab__close { display: none; }
body.ia-open .ia-fab__open  { display: none; }
body.ia-open .ia-fab__close { display: block; }

/* Unread/attention dot */
.ia-fab__dot {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #e74c3c;
    border: 2px solid var(--white);
    display: none;
}
.ia-fab.is-attention .ia-fab__dot { display: block; }

/* ----------------------------------------------------------------- panel -- */

/* DESKTOP GEOMETRY — measured, like the mobile block below.

   At 1366x768 the storefront's sticky chrome stacks up to y=162:

       .top-bar             30px   declared in style.css (height: 30px)
       .navbar              80px   declared in style.css (height: 80px)
       .min-order-wrapper   52px   content-driven; only its padding is declared
       ------------------------------------------------------------------
       total                162px

   Two of the three are readable straight out of style.css, so the test
   re-derives 110px from them and checks the clearance leaves room for the
   notice bar on top. The notice bar itself cannot be computed from CSS — it
   sizes to its text — so 52px is a measured allowance, called out here rather
   than buried in a magic number.

   The panel previously used bottom:100px with height:540px, which on a 768px
   laptop put its top edge at y=80 — 82px inside the header, over the cart
   icon and a pixel away from the About/Contact links.

       --ia-top-clear     162 chrome + 8 breathing room
       --ia-bottom-clear   90 (FAB top edge: bottom 30 + height 60) + 16

   Height is a comfortable fixed size rather than a percentage: on a 1080px
   screen a 75%-tall chat panel is mostly empty space. It shrinks on short
   laptops via max-height, and never rises into the header. */

.ia-panel {
    --ia-top-clear: 170px;
    --ia-bottom-clear: 106px;

    position: fixed;
    bottom: var(--ia-bottom-clear);
    right: 30px;
    z-index: 950;
    width: 380px;
    max-width: calc(100vw - 40px);

    height: 480px;
    max-height: calc(100vh - var(--ia-top-clear) - var(--ia-bottom-clear));

    background: var(--white);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.18);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(12px) scale(0.98);
    pointer-events: none;
    transition: opacity var(--transition-speed) ease,
                transform var(--transition-speed) ease;
}

body.ia-open .ia-panel {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

.ia-panel[hidden] { display: none; }

/* ---------------------------------------------------------------- header -- */

.ia-head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    background: var(--primary-color);
    color: var(--white);
    flex-shrink: 0;
}

.ia-head__title { font-size: 1rem; font-weight: 600; flex: 1; }

.ia-head__btn {
    background: rgba(255, 255, 255, 0.18);
    border: none;
    color: var(--white);
    width: 30px;
    height: 30px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    justify-content: center;
}
.ia-head__btn:hover { background: rgba(255, 255, 255, 0.32); }

/* ----------------------------------------------------------------- state -- */

.ia-state {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 16px;
    font-size: 0.78rem;
    background: var(--light-grey);
    border-bottom: 1px solid var(--border-color);
    color: #666;
    flex-shrink: 0;
}

.ia-state__dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #9e9e9e;
    flex-shrink: 0;
}

.ia-state[data-state="ready"]     .ia-state__dot { background: #4caf50; }
.ia-state[data-state="processing"] .ia-state__dot,
.ia-state[data-state="searching"]  .ia-state__dot,
.ia-state[data-state="acting"]     .ia-state__dot {
    background: var(--primary-color);
    animation: ia-pulse 1s infinite;
}
.ia-state[data-state="completed"] .ia-state__dot { background: #4caf50; }
.ia-state[data-state="waiting"]   .ia-state__dot { background: #ff9800; }
.ia-state[data-state="error"]     .ia-state__dot { background: #e74c3c; }

@keyframes ia-pulse {
    0%, 100% { opacity: 1; }
    50%      { opacity: 0.3; }
}

/* -------------------------------------------------------------- messages -- */

.ia-log {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: #fafafa;

    /* A flex child shrinks past its content by default, and the voice block
       below can grow — the permission instructions are four steps plus two
       paragraphs. Measured on a 375x667 iPhone SE, opening them collapsed this
       log from 197px to 50px: the customer who most needs to read (blocked
       mic, instructions on screen, "just type instead" as the way out) lost
       the conversation entirely. The floor stops that. */
    min-height: 110px;
}

.ia-msg {
    max-width: 85%;
    padding: 9px 13px;
    border-radius: 14px;
    font-size: 0.88rem;
    line-height: 1.45;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
}

.ia-msg--user {
    align-self: flex-end;
    background: var(--primary-color);
    color: var(--white);
    border-bottom-right-radius: 4px;
}

.ia-msg--agent {
    align-self: flex-start;
    background: var(--white);
    color: var(--secondary-color);
    border: 1px solid var(--border-color);
    border-bottom-left-radius: 4px;
}

.ia-msg--system {
    align-self: center;
    background: #fff4e5;
    color: #8a5a00;
    border: 1px solid #ffd9a0;
    font-size: 0.8rem;
    max-width: 95%;
    text-align: center;
}

.ia-msg--error {
    align-self: center;
    background: #ffeaea;
    color: #b02020;
    border: 1px solid #ffc9c9;
    font-size: 0.8rem;
    max-width: 95%;
    text-align: center;
}

/* --------------------------------------------------------- product cards -- */

.ia-cards {
    align-self: stretch;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.ia-card {
    display: flex;
    gap: 10px;
    align-items: center;
    background: var(--white);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 8px;
}

.ia-card__img {
    width: 48px;
    height: 48px;
    object-fit: contain;
    border-radius: 8px;
    background: var(--light-grey);
    flex-shrink: 0;
}

.ia-card__body { flex: 1; min-width: 0; }

.ia-card__title {
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--secondary-color);
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ia-card__meta { font-size: 0.75rem; color: #777; }
.ia-card__price { font-weight: 600; color: var(--secondary-color); }
.ia-card__oos { color: #b02020; }

.ia-card__add {
    background: var(--primary-color);
    color: var(--white);
    border: none;
    border-radius: 16px;
    padding: 6px 13px;
    font-size: 0.76rem;
    font-weight: 500;
    cursor: pointer;
    flex-shrink: 0;
}
.ia-card__add:hover { filter: brightness(0.93); }
.ia-card__add:disabled { background: #bbb; cursor: default; }

/* --------------------------------------------------- confirmation card ----
   Built now, unused in Phase 1. Phase 3 renders this for anything high risk:
   the model may propose the action and fill this in, but only a real click on
   .ia-confirm__yes may ever trigger it.
   -------------------------------------------------------------------------- */

.ia-confirm {
    align-self: stretch;
    background: var(--white);
    border: 2px solid var(--primary-color);
    border-radius: 12px;
    padding: 12px;
}

.ia-confirm__title {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--secondary-color);
    margin-bottom: 8px;
}

.ia-confirm__lines {
    list-style: none;
    margin: 0 0 10px 0;
    padding: 0;
    font-size: 0.82rem;
    color: #555;
}

.ia-confirm__lines li {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    padding: 3px 0;
    border-bottom: 1px dashed var(--border-color);
}
.ia-confirm__lines li:last-child { border-bottom: none; font-weight: 600; color: var(--secondary-color); }

.ia-confirm__actions { display: flex; gap: 8px; }

.ia-confirm__yes,
.ia-confirm__no {
    flex: 1;
    border: none;
    border-radius: 8px;
    padding: 9px;
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
}

.ia-confirm__yes { background: var(--primary-color); color: var(--white); }
.ia-confirm__no  { background: var(--light-grey); color: var(--secondary-color); }
.ia-confirm__yes:disabled,
.ia-confirm__no:disabled { opacity: 0.5; cursor: default; }

/* ------------------------------------------------------- order confirm ---- */

.ia-order {
    border: 1px solid #f0c98a;
    background: #fffdf7;
    border-radius: 10px;
    padding: 12px;
    font-size: 0.82rem;
}

.ia-order__head {
    font-weight: 700;
    margin-bottom: 8px;
    color: #7a4d00;
}

.ia-order__items { margin-bottom: 8px; }

.ia-order__row {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    padding: 2px 0;
}
.ia-order__row span:first-child { color: #555; min-width: 0; }
.ia-order__row span:last-child  { white-space: nowrap; font-variant-numeric: tabular-nums; }

.ia-order__sums {
    border-top: 1px dashed #e4cfa5;
    padding-top: 6px;
}
.ia-order__row.is-total {
    border-top: 1px solid #e4cfa5;
    margin-top: 4px;
    padding-top: 6px;
    font-weight: 700;
    color: #222;
}

.ia-order__addr {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px dashed #e4cfa5;
    color: #555;
    line-height: 1.4;
}
.ia-order__pay { margin-top: 4px; font-weight: 600; color: #7a4d00; }

.ia-order__note {
    margin: 8px 0 0;
    font-size: 0.75rem;
    color: #8a6d3b;
    font-style: italic;
}

.ia-order__actions {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

.ia-order__confirm,
.ia-order__cancel {
    border: none;
    border-radius: 6px;
    padding: 10px 12px;
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
}

/* The confirm button takes the space, because it is the one being decided on
   — but Cancel is a real button beside it, not a link hidden underneath. */
.ia-order__confirm {
    flex: 1;
    background: var(--primary-color);
    color: var(--white);
}
.ia-order__cancel {
    background: #fff;
    border: 1px solid var(--border-color);
    color: #666;
}
.ia-order__confirm:disabled,
.ia-order__cancel:disabled { opacity: 0.5; cursor: default; }

.ia-order__status {
    margin-top: 8px;
    font-size: 0.8rem;
    min-height: 1px;
}
.ia-order[data-done="placed"]    { border-color: #9ad3a0; background: #f6fdf7; }
.ia-order[data-done="placed"]    .ia-order__status { color: #1d6f2b; font-weight: 600; }
.ia-order[data-done="failed"]    .ia-order__status,
.ia-order[data-done="unknown"]   .ia-order__status { color: #b8860b; }
.ia-order[data-done="cancelled"] { border-color: var(--border-color); background: #fafafa; }
.ia-order[data-done="cancelled"] .ia-order__status { color: #777; }

.ia-order__link { color: var(--primary-color); font-weight: 600; }

/* --------------------------------------------------------------- voice ---- */

/* Sits between the log and the composer, inside the panel, so it inherits the
   panel's stacking context and adds no new z-index of its own. */
.ia-voice {
    flex-shrink: 0;
    border-top: 1px solid var(--border-color);
    background: var(--white);
    padding: 8px 10px 0;
}

.ia-voice__row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.ia-voice__mic {
    position: relative;
    /* 44px, not 42: it is the primary action of this block and 44 is the
       smallest reliable touch target. Measured at 41px with the earlier value
       once box-sizing was applied. */
    width: 44px;
    height: 44px;
    box-sizing: border-box;
    border: none;
    border-radius: 50%;
    background: var(--primary-color);
    color: var(--white);
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
}

/* While recording, --ia-progress runs 0 -> 1 and paints the elapsed share of
   the 15 seconds around the button. A conic gradient means the countdown needs
   no extra element and no animation frame. */
.ia-voice[data-mode="recording"] .ia-voice__mic {
    background: #d64545;
    background-image: conic-gradient(
        rgba(255, 255, 255, 0.32) calc(var(--ia-progress, 0) * 360deg),
        transparent 0
    );
}

.ia-voice[data-mode="requesting"] .ia-voice__mic,
.ia-voice[data-mode="sending"] .ia-voice__mic {
    background: #bbb;
    cursor: default;
}

.ia-voice[data-mode="blocked"] .ia-voice__mic {
    background: #bbb;
}

.ia-voice__ring {
    position: absolute;
    inset: -4px;
    border-radius: 50%;
    border: 2px solid #d64545;
    opacity: 0;
}
.ia-voice[data-mode="recording"] .ia-voice__ring {
    opacity: 1;
    animation: ia-voice-pulse 1.4s ease-out infinite;
}

@keyframes ia-voice-pulse {
    0%   { transform: scale(1);    opacity: 0.9; }
    100% { transform: scale(1.35); opacity: 0; }
}

.ia-voice__timer {
    font-size: 0.78rem;
    color: #d64545;
    font-variant-numeric: tabular-nums;
    min-width: 38px;
}

.ia-voice__note {
    font-size: 0.76rem;
    color: #777;
    min-height: 16px;
    margin-bottom: 4px;
}
.ia-voice__note[data-kind="warn"]  { color: #b8860b; }
.ia-voice__note[data-kind="error"] { color: #c0392b; }

/* When the permission instructions are up, the note above them says the same
   thing their heading does. Dropping the duplicate is worth ~20px, which on a
   short screen is the difference between the log showing a message and showing
   a sliver of one. */
.ia-voice[data-mode="blocked"] .ia-voice__note { display: none; }

.ia-voice__speak,
.ia-voice__gender {
    width: 32px;
    height: 32px;
    border: 1px solid var(--border-color);
    border-radius: 50%;
    background: var(--white);
    color: #777;
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
}
.ia-voice__speak[hidden],
.ia-voice__gender[hidden] { display: none; }

.ia-voice[data-speak="on"] .ia-voice__speak {
    background: var(--primary-color);
    border-color: var(--primary-color);
    color: var(--white);
}

.ia-voice__gender { margin-left: auto; font-weight: 600; }

/* Permission instructions. Shown only after a real denial, because the browser
   will not ask again and the setting has to be changed by hand. */
.ia-voice__help {
    border: 1px solid #f0d9a0;
    background: #fdf7e6;
    border-radius: 8px;
    padding: 8px 10px;
    margin-bottom: 8px;
    font-size: 0.75rem;
    color: #6b5720;

    /* Scrolls inside itself rather than pushing the log out of the panel.
       The panel is a fixed height, so a min-height on the log alone loses this
       argument — the log simply gets clamped. The cap has to be on the block
       that grows, and it has to scale with the screen: 13vh keeps the log
       above its floor on a 667px iPhone SE while still showing three steps at
       once, and the numbered list makes scrolling for the rest obvious. */
    max-height: min(150px, 13vh);
    overflow-y: auto;
}
.ia-voice__help[hidden] { display: none; }

.ia-voice__help-title { margin: 0 0 6px; font-weight: 600; }
.ia-voice__help ol    { margin: 0; padding-left: 18px; }
.ia-voice__help li    { margin-bottom: 3px; }
.ia-voice__help-alt   { margin: 6px 0 0; font-style: italic; }

@media (prefers-reduced-motion: reduce) {
    .ia-voice[data-mode="recording"] .ia-voice__ring { animation: none; opacity: 0.9; }
}

/* ------------------------------------------------------------ composer ---- */

.ia-form {
    display: flex;
    gap: 8px;
    padding: 10px;
    border-top: 1px solid var(--border-color);
    background: var(--white);
    flex-shrink: 0;
}

.ia-form__input {
    flex: 1;
    min-width: 0;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: 9px 14px;
    font-size: 0.88rem;
    outline: none;
}
.ia-form__input:focus { border-color: var(--primary-color); }

.ia-form__send {
    width: 38px;
    height: 38px;
    border: none;
    border-radius: 50%;
    background: var(--primary-color);
    color: var(--white);
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}
.ia-form__send:disabled { background: #bbb; cursor: default; }

.ia-form__count {
    position: absolute;
    bottom: 2px;
    right: 60px;
    font-size: 0.68rem;
    color: #aaa;
    pointer-events: none;
}
.ia-form__count.is-over { color: #e74c3c; }

/* -------------------------------------------------------------- mobile ----

   MEASURED, NOT GUESSED. On a 375x667 viewport the storefront puts:

       fixed header      y   30 -> 141   (top bar 0-30, navbar 30-90,
                                          delivery/min-order notice to 141)
       WhatsApp float    y  577 -> 637   x 30 -> 90, bottom-left
       our FAB           y  595 -> 647   x 303 -> 355, bottom-right

   The panel must clear both. Two named clearances make the intent readable
   and keep the two magic numbers in one place:

       --ia-top-clear     header bottom (141) + 8px breathing room
       --ia-bottom-clear  WhatsApp top edge (90 from the bottom) + 8px

   WhatsApp is the shop's real lead channel, so the panel is lifted ABOVE it
   rather than narrowed around it — at 320px a panel that dodged it sideways
   would be about 210px wide, which is not a usable chat.

   Height is anchored from the bottom so the composer never drifts, and capped
   at 72vh. On 667px the clearances bind first and the panel lands at ~63% of
   the viewport, which is what leaves the page visible behind it.
   -------------------------------------------------------------------------- */

@media (max-width: 480px) {
    .ia-panel {
        --ia-top-clear: 149px;
        --ia-bottom-clear: 98px;

        left: 16px;
        right: 16px;
        width: auto;
        max-width: none;

        top: auto;
        bottom: var(--ia-bottom-clear);

        /* Fill what is left between the header and the WhatsApp button... */
        height: calc(100vh - var(--ia-top-clear) - var(--ia-bottom-clear));
        /* ...but never more than three quarters of the screen. */
        max-height: 72vh;
    }

    .ia-fab {
        bottom: 20px;
        right: 20px;
        width: 52px;
        height: 52px;
        font-size: 1.3rem;
    }

    .ia-msg { max-width: 90%; font-size: 0.85rem; }
    .ia-card__title { font-size: 0.78rem; }
}

/* 320px: the same clearances hold, but the margins tighten so the chat keeps
   a usable width. */
@media (max-width: 360px) {
    .ia-panel {
        left: 10px;
        right: 10px;
    }

    .ia-card__img { width: 40px; height: 40px; }
    .ia-card__add { padding: 6px 10px; }
}

/* Very short screens — a landscape phone, or a keyboard eating the viewport.
   Clearing the whole header would leave nothing, so the top clearance drops to
   the navbar and the cap goes with it. WhatsApp is still cleared. */
@media (max-height: 560px) {
    .ia-panel {
        --ia-top-clear: 96px;
        max-height: 78vh;
    }
}

@media (prefers-reduced-motion: reduce) {
    .ia-panel, .ia-fab { transition: none; }
    .ia-state__dot { animation: none; }
}
