/* =========================================================================
   e-PriceScan v3 — modern, mobile-first styling
   Light + dark aware, finger-friendly (min 48px touch targets).
   ========================================================================= */

/* The HTML `hidden` attribute must always win over component display rules
   (e.g. .sheet-backdrop is display:flex), otherwise hidden dialogs show. */
[hidden] { display: none !important; }

:root {
    --bg: #f1f5f9;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --border: #e2e8f0;
    --text: #0f172a;
    --muted: #64748b;
    --primary: #2563eb;
    --primary-ink: #ffffff;
    --primary-dark: #1d4ed8;
    --like: #16a34a;
    --buy: #f59e0b;
    --dislike: #dc2626;
    --danger: #dc2626;
    --shadow: 0 1px 3px rgba(15, 23, 42, .1), 0 1px 2px rgba(15, 23, 42, .06);
    --shadow-lg: 0 20px 40px rgba(15, 23, 42, .25);
    --radius: 14px;
    --radius-sm: 10px;
    --tap: 52px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0b1220;
        --surface: #131c2e;
        --surface-2: #0f1727;
        --border: #263349;
        --text: #e6edf7;
        --muted: #94a3b8;
        --primary: #3b82f6;
        --primary-dark: #2563eb;
        --shadow: 0 1px 3px rgba(0, 0, 0, .4);
        --shadow-lg: 0 20px 40px rgba(0, 0, 0, .6);
    }
}
:root[data-theme="dark"] {
    --bg: #0b1220; --surface: #131c2e; --surface-2: #0f1727; --border: #263349;
    --text: #e6edf7; --muted: #94a3b8; --primary: #3b82f6; --primary-dark: #2563eb;
    --shadow: 0 1px 3px rgba(0,0,0,.4); --shadow-lg: 0 20px 40px rgba(0,0,0,.6);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.45;
    -webkit-text-size-adjust: 100%;
}

input, button, select { font-family: inherit; font-size: 16px; }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .5rem;
    min-height: var(--tap); padding: 0 1.15rem;
    border: 1px solid transparent; border-radius: var(--radius-sm);
    background: var(--surface-2); color: var(--text);
    font-weight: 600; cursor: pointer; user-select: none;
    transition: transform .05s ease, background .15s ease, box-shadow .15s ease;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-primary:active { background: var(--primary-dark); }
.btn-block { width: 100%; }
.btn[disabled] { opacity: .55; pointer-events: none; }

.icon-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: var(--tap); height: var(--tap); min-width: var(--tap);
    border: none; background: transparent; color: inherit;
    font-size: 1.5rem; border-radius: 12px; cursor: pointer;
}
.icon-btn:active { background: rgba(127, 127, 127, .18); }

.topbar-link {
    min-height: 44px; padding: 0 .9rem; border: 1px solid var(--border); border-radius: 10px;
    background: transparent; color: var(--danger); font: inherit; font-size: .9rem; font-weight: 600;
    cursor: pointer;
}
.topbar-link:active { background: rgba(127, 127, 127, .18); }

/* ---------- Login ---------- */
.auth {
    min-height: 100dvh; max-width: 440px; margin: 0 auto;
    padding: calc(var(--safe-top) + 2.5rem) 1.25rem calc(var(--safe-bottom) + 1.5rem);
    display: flex; flex-direction: column; justify-content: center; gap: 1.5rem;
}
.brand { text-align: center; }
.brand-mark {
    width: 74px; height: 74px; margin: 0 auto .75rem;
    display: grid; place-items: center;
    background: linear-gradient(135deg, var(--primary), #7c3aed);
    color: #fff; font-weight: 800; font-size: 1.6rem; border-radius: 20px;
    box-shadow: var(--shadow-lg);
}
.brand-mark span { opacity: .8; }
.brand h1 { margin: 0; font-size: 1.6rem; letter-spacing: -.02em; }
.tagline { margin: .15rem 0 0; color: var(--muted); font-size: .95rem; }

.card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 1.25rem; display: flex; flex-direction: column; gap: 1rem;
}
.field { display: flex; flex-direction: column; gap: .4rem; }
.field label { font-size: .82rem; font-weight: 600; color: var(--muted); }
.field input {
    min-height: var(--tap); padding: 0 .9rem;
    background: var(--surface-2); color: var(--text);
    border: 1.5px solid var(--border); border-radius: var(--radius-sm);
    outline: none; transition: border-color .15s ease, box-shadow .15s ease;
}
.field input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 22%, transparent);
}
.foot { text-align: center; color: var(--muted); font-size: .78rem; margin: 0; }

.msg { margin: 0; font-size: .88rem; padding: .6rem .75rem; border-radius: var(--radius-sm); }
.msg.error { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }
.muted { color: var(--muted); font-size: .9rem; }

/* ---------- App shell ---------- */
.screen-app { min-height: 100dvh; }
.topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: .25rem;
    padding: var(--safe-top) .35rem 0;
    min-height: calc(56px + var(--safe-top));
    background: var(--surface); border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
}
.topbar-title { flex: 1; min-width: 0; text-align: center; line-height: 1.1; }
.topbar-title span { display: block; font-weight: 700; font-size: 1.02rem;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-title small { color: var(--muted); font-size: .74rem; text-transform: uppercase; letter-spacing: .04em; }

.view { max-width: 560px; margin: 0 auto; padding: 1rem 1rem calc(1.5rem + var(--safe-bottom)); }

/* ---------- Scan screen ---------- */
.scan-form { display: flex; flex-direction: column; gap: 1rem; margin-top: .5rem; }
.mode-toggle {
    display: flex; padding: 5px; gap: 6px;
    background: var(--surface-2); border: 1px solid var(--border); border-radius: 14px;
}
.mode {
    flex: 1; min-height: var(--tap); border: 2px solid transparent; border-radius: 10px;
    background: transparent; color: var(--muted); font: inherit; font-size: 1.05rem; font-weight: 600;
    cursor: pointer; transition: background .15s, color .15s, box-shadow .15s;
}
/* Active mode is unmistakable: solid brand fill, white bold text, glow. */
.mode.active {
    background: var(--primary); color: var(--primary-ink); font-weight: 800;
    border-color: var(--primary-dark);
    box-shadow: 0 4px 14px color-mix(in srgb, var(--primary) 45%, transparent);
}
.mode.active::before { content: "\2713\00a0"; }

.scan-input { display: flex; gap: .5rem; }
.scan-input input {
    flex: 1; min-height: 58px; padding: 0 1rem; text-align: center;
    font-size: 1.15rem; letter-spacing: .02em;
    background: var(--surface); color: var(--text);
    border: 2px solid var(--primary); border-radius: 14px; outline: none;
}
.scan-input .btn { min-height: 58px; padding: 0 1.4rem; border-radius: 14px; }
.hint { color: var(--muted); font-size: .85rem; text-align: center; margin: 0; }
.store-image { display: block; max-width: 220px; margin: 1rem auto 0; border-radius: 12px; }

/* Quantity result table */
.stock-table { width: 100%; border-collapse: collapse; margin-top: 1rem;
    background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.stock-table th, .stock-table td { padding: .8rem .9rem; text-align: left; border-bottom: 1px solid var(--border); }
.stock-table th { background: var(--surface-2); font-size: .78rem; text-transform: uppercase; color: var(--muted); }
.stock-table td:last-child, .stock-table th:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.stock-table tr:last-child td { border-bottom: none; }

/* ---------- Item screen ---------- */
.item-image {
    position: relative; text-align: center; border-radius: var(--radius);
    overflow: hidden; margin-bottom: 1rem;
}
.item-image img { width: 100%; max-height: 46vh; object-fit: contain; display: block; }
.item-price {
    display: flex; align-items: baseline; gap: .75rem; flex-wrap: wrap;
    padding: .5rem 0 1rem;
}
.price-ours { font-size: 2rem; font-weight: 800; color: var(--text); letter-spacing: -.02em; }
.price-suggest { color: var(--muted); text-decoration: line-through; font-size: 1.05rem; }
.price-quote { margin-left: auto; }
.price-quote label { display: block; font-size: .72rem; color: var(--muted); }
.price-quote input {
    width: 120px; min-height: 44px; text-align: right; padding: 0 .6rem;
    border: 1.5px solid var(--border); border-radius: 10px; background: var(--surface-2); color: var(--text);
}

.spec-list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.spec-row { display: flex; justify-content: space-between; gap: 1rem; padding: .8rem 1rem; border-bottom: 1px solid var(--border); }
.spec-row:last-child { border-bottom: none; }
.spec-row .k { color: var(--muted); font-size: .9rem; flex-shrink: 0; }
.spec-row .v { text-align: right; font-weight: 600; }

/* Sticky Like / Buy / Dislike bar */
.item-actions {
    position: sticky; bottom: 0;
    display: grid; grid-template-columns: 1fr 1fr 1fr; gap: .6rem;
    max-width: 560px; margin: 1rem auto 0;
    padding: .75rem 1rem calc(.75rem + var(--safe-bottom));
    background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.item-actions .btn { min-height: 56px; font-size: 1rem; color: #fff; }
.item-actions .like { background: var(--like); }
.item-actions .buy { background: var(--buy); color: #1c1400; }
.item-actions .dislike { background: var(--dislike); }
.item-actions .btn.done { opacity: .45; }

/* ---------- Bottom sheet ---------- */
.sheet-backdrop {
    position: fixed; inset: 0; z-index: 50;
    background: rgba(15, 23, 42, .5); backdrop-filter: blur(2px);
    display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
    width: 100%; max-width: 560px;
    background: var(--surface); border-radius: 20px 20px 0 0;
    box-shadow: var(--shadow-lg);
    padding: 1.25rem 1.25rem calc(1.25rem + var(--safe-bottom));
    max-height: 88dvh; overflow-y: auto;
    animation: sheet-up .22s ease;
}
.sheet-tall { min-height: 55dvh; }
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: .5rem; }
.sheet h2 { margin: 0 0 .5rem; font-size: 1.2rem; }
.screen-login .sheet-backdrop { align-items: center; padding: 1rem; }
.screen-login .sheet { border-radius: 20px; }

/* ---------- Stats ---------- */
.stats-card {
    margin-top: 1.25rem; padding: 1rem; background: var(--surface);
    border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.stats-card-head { display: flex; align-items: center; justify-content: space-between; gap: .75rem; margin-bottom: .85rem; }
.stats-card-head h2 { margin: 0; font-size: 1.05rem; }
.stats-card-head small { display: block; margin-top: .15rem; font-size: .78rem; }
.btn-refresh {
    min-height: 48px; padding: 0 1rem; flex-shrink: 0;
    background: var(--surface-2); border: 1px solid var(--border); color: var(--primary); font-weight: 700;
}
.btn-refresh .refresh-icon { display: inline-block; font-size: 1.15rem; margin-right: .2rem; }
.btn-refresh.loading .refresh-icon { animation: spin .8s linear infinite; }
.stats-body { display: flex; flex-direction: column; gap: 1rem; }
.stats-grid {
    display: grid; grid-template-columns: 1.4fr 1fr 1fr 1fr; gap: 1px;
    background: var(--border); border: 1px solid var(--border);
    border-radius: 12px; overflow: hidden;
}
.stats-grid > div { background: var(--surface); padding: .7rem .55rem; font-size: .9rem; }
.stats-grid .head { background: var(--surface-2); font-size: .72rem; text-transform: uppercase; color: var(--muted); text-align: center; }
.stats-grid .rowlabel { font-weight: 600; }
.stats-grid .num { text-align: center; font-variant-numeric: tabular-nums; }
.stats-grid .num b {
    display: inline-block; min-width: 2ch; padding: .15rem .45rem;
    border-radius: 8px; background: var(--surface-2); font-weight: 700;
}
.stats-grid .num.hot b { background: color-mix(in srgb, var(--buy) 26%, transparent); }
.stat-line { display: flex; justify-content: space-between; align-items: center;
    padding: .75rem .9rem; background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; }
.stat-line .badge { background: var(--primary); color: #fff; border-radius: 999px; padding: .1rem .6rem; font-weight: 700; font-size: .85rem; }

/* ---------- Misc ---------- */
.spinner { width: 34px; height: 34px; margin: 2rem auto; border: 3px solid var(--border);
    border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
    position: fixed; left: 50%; bottom: calc(1.5rem + var(--safe-bottom)); transform: translateX(-50%);
    z-index: 80; background: var(--text); color: var(--bg);
    padding: .7rem 1.1rem; border-radius: 999px; font-weight: 600; font-size: .9rem;
    box-shadow: var(--shadow-lg); animation: toast-in .2s ease;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }
