:root {
    --ph-primary: #0f766e;
    --ph-primary-dark: #115e59;
    --ph-primary-light: #14b8a6;
    --ph-accent: #f59e0b;
    --ph-bg: #f1f5f9;
    --ph-sidebar: #0b3b39;
    --ph-text: #1e293b;
    --ph-muted: #64748b;
    --ph-border: #e2e8f0;
}

html, body {
    height: 100%;
    overflow-x: hidden;
}

body {
    font-family: "Segoe UI", "Tahoma", "Cairo", sans-serif;
    background: var(--ph-bg);
    color: var(--ph-text);
    font-size: 14.5px;
    -webkit-tap-highlight-color: rgba(15, 118, 110, .12);
}

a {
    text-decoration: none;
}

/* ---------- الهيكل العام ---------- */
.app-shell {
    display: flex;
    min-height: 100vh;
}

.app-sidebar {
    width: 250px;
    flex-shrink: 0;
    background: linear-gradient(180deg, var(--ph-sidebar) 0%, #072a28 100%);
    color: #cbd5e1;
    position: sticky;
    top: 0;
    height: 100vh;
    height: 100dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.app-brand {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: 1.1rem 1.15rem;
    color: #fff;
    font-weight: 700;
    font-size: 1.1rem;
    border-bottom: 1px solid rgba(255, 255, 255, .08);
}

.app-brand i {
    font-size: 1.5rem;
    color: var(--ph-primary-light);
}

.app-nav {
    padding: .6rem 0 3rem;
}

.app-nav .nav-section {
    padding: .9rem 1.15rem .35rem;
    font-size: .72rem;
    letter-spacing: .04em;
    color: #64809a;
    text-transform: uppercase;
}

.app-nav a {
    display: flex;
    align-items: center;
    gap: .65rem;
    padding: .58rem 1.15rem;
    color: #cbd5e1;
    font-size: .93rem;
    border-right: 3px solid transparent;
    transition: background .15s, color .15s;
}

.app-nav a:hover {
    background: rgba(255, 255, 255, .06);
    color: #fff;
}

.app-nav a.active {
    background: rgba(20, 184, 166, .16);
    color: #fff;
    border-right-color: var(--ph-primary-light);
    font-weight: 600;
}

.app-nav a i {
    width: 1.15rem;
    text-align: center;
    font-size: 1.02rem;
}

.app-nav .badge {
    margin-inline-start: auto;
}

.app-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.app-topbar {
    background: #fff;
    border-bottom: 1px solid var(--ph-border);
    padding: .55rem 1.1rem;
    display: flex;
    align-items: center;
    gap: .75rem;
    position: sticky;
    top: 0;
    z-index: 1020;
    min-height: 52px;
}

.app-content {
    padding: 1.1rem;
    flex: 1;
}

.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: .6rem;
    margin-bottom: 1rem;
}

.page-head h1 {
    font-size: 1.32rem;
    font-weight: 700;
    margin: 0;
}

.page-head .subtitle {
    color: var(--ph-muted);
    font-size: .85rem;
    margin: .15rem 0 0;
}

/* ---------- الكروت ---------- */
.card {
    border: 1px solid var(--ph-border);
    border-radius: .65rem;
    box-shadow: 0 1px 2px rgba(15, 23, 42, .04);
}

.card-header {
    background: #fff;
    border-bottom: 1px solid var(--ph-border);
    font-weight: 600;
    padding: .7rem 1rem;
}

.stat-card {
    background: #fff;
    border: 1px solid var(--ph-border);
    border-radius: .7rem;
    padding: .95rem 1.05rem;
    height: 100%;
    display: flex;
    align-items: center;
    gap: .9rem;
}

.stat-card .stat-icon {
    width: 46px;
    height: 46px;
    border-radius: .7rem;
    display: grid;
    place-items: center;
    font-size: 1.35rem;
    flex-shrink: 0;
}

.stat-card .stat-value {
    font-size: 1.32rem;
    font-weight: 700;
    line-height: 1.1;
}

.stat-card .stat-label {
    color: var(--ph-muted);
    font-size: .82rem;
}

.bg-soft-primary { background: #ccfbf1; color: var(--ph-primary-dark); }
.bg-soft-success { background: #dcfce7; color: #15803d; }
.bg-soft-warning { background: #fef3c7; color: #b45309; }
.bg-soft-danger { background: #fee2e2; color: #b91c1c; }
.bg-soft-info { background: #dbeafe; color: #1d4ed8; }

/* ---------- الجداول ---------- */
.table {
    font-size: .9rem;
    margin-bottom: 0;
}

.table > thead {
    background: #f8fafc;
}

.table > thead th {
    font-weight: 600;
    color: #475569;
    font-size: .82rem;
    white-space: nowrap;
    border-bottom: 1px solid var(--ph-border);
}

.table > tbody td {
    vertical-align: middle;
}

.table-hover > tbody > tr:hover > * {
    background: #f0fdfa;
}

.num {
    font-variant-numeric: tabular-nums;
    direction: ltr;
    text-align: center;
}

.ltr {
    direction: ltr;
    text-align: start;
}

/* ---------- الأزرار ---------- */
.btn-primary {
    background: var(--ph-primary);
    border-color: var(--ph-primary);
}

.btn-primary:hover, .btn-primary:focus {
    background: var(--ph-primary-dark);
    border-color: var(--ph-primary-dark);
}

.btn-outline-primary {
    color: var(--ph-primary);
    border-color: var(--ph-primary);
}

.btn-outline-primary:hover {
    background: var(--ph-primary);
    border-color: var(--ph-primary);
}

.btn {
    border-radius: .45rem;
}

/* ---------- تسجيل الدخول ---------- */
.login-wrap {
    min-height: 100vh;
    display: grid;
    place-items: center;
    background: linear-gradient(135deg, #0f766e 0%, #0b3b39 55%, #082f2c 100%);
    padding: 1.5rem;
}

.login-card {
    width: 100%;
    max-width: 410px;
    background: #fff;
    border-radius: 1rem;
    padding: 2rem 1.9rem;
    box-shadow: 0 20px 45px rgba(0, 0, 0, .28);
}

.login-logo {
    width: 62px;
    height: 62px;
    border-radius: 1rem;
    background: #ccfbf1;
    color: var(--ph-primary-dark);
    display: grid;
    place-items: center;
    font-size: 2rem;
    margin: 0 auto 1rem;
}

/* ---------- الماسح والباركود ---------- */
#scanner-view {
    width: 100%;
    max-width: 420px;
    margin: 0 auto;
    border-radius: .6rem;
    overflow: hidden;
    background: #000;
}

.scan-hint {
    color: var(--ph-muted);
    font-size: .85rem;
    text-align: center;
    margin-top: .6rem;
}

.barcode-label {
    border: 1px dashed #94a3b8;
    border-radius: .4rem;
    padding: .45rem;
    text-align: center;
    background: #fff;
    break-inside: avoid;
}

.product-name-en {
    font-size: .8rem;
    color: var(--ph-muted);
    direction: ltr;
    unicode-bidi: isolate;
}

.barcode-label .name {
    font-size: .72rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.barcode-label .name-en {
    font-size: .62rem;
    color: #64748b;
    direction: ltr;
    unicode-bidi: isolate;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.barcode-label .code {
    font-size: .72rem;
    direction: ltr;
    font-variant-numeric: tabular-nums;
}

.barcode-label svg {
    width: 100%;
    height: 46px;
}

.barcode-label svg.barcode-2d {
    height: 90px;
    width: 90px;
}

/* ---------- نقطة البيع ---------- */
.pos-grid {
    display: grid;
    grid-template-columns: 1fr 330px;
    gap: 1rem;
    align-items: start;
}

.pos-total {
    font-size: 1.85rem;
    font-weight: 700;
    color: var(--ph-primary-dark);
}

#cartTable tbody tr.just-added {
    animation: cartFlash .85s ease;
}

@keyframes cartFlash {
    from { background: #ccfbf1; }
    to { background: transparent; }
}

.expiry-near { color: #b45309; font-weight: 600; }
.expiry-over { color: #b91c1c; font-weight: 600; }

.search-result-item {
    cursor: pointer;
}

.ai-cam {
    width: 100%;
    max-height: 320px;
    object-fit: cover;
    border-radius: .6rem;
    background: #000;
    margin-bottom: .75rem;
}

.ai-drop {
    display: block;
    border: 1.5px dashed var(--ph-border);
    border-radius: .7rem;
    padding: 1.1rem;
    text-align: center;
    color: var(--ph-muted);
    cursor: pointer;
    background: #f8fafc;
}

.ai-drop:hover {
    border-color: var(--ph-primary-light);
    color: var(--ph-primary-dark);
    background: #f0fdfa;
}

.ai-previews {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: .6rem;
}

.ai-preview {
    position: relative;
    border-radius: .5rem;
    overflow: hidden;
    border: 1px solid var(--ph-border);
}

.ai-preview img {
    width: 100%;
    height: 110px;
    object-fit: cover;
    display: block;
}

.ai-preview-del {
    position: absolute;
    top: .25rem;
    inset-inline-start: .25rem;
}

/* ---------- الطباعة ---------- */
@media print {
    .app-sidebar, .app-topbar, .no-print {
        display: none !important;
    }

    .app-content {
        padding: 0;
    }

    body {
        background: #fff;
    }

    .card {
        border: none;
        box-shadow: none;
    }
}

.sidebar-backdrop {
    display: none;
}

.pos-grid .col-md-7,
.pos-grid .col-md-5 {
    position: relative;
}

#results {
    inset-inline: .75rem;
    width: auto;
}

/* ---------- موبايل وتابلت ---------- */
@media (max-width: 991.98px) {
    body {
        font-size: 15px;
    }

    .app-shell {
        display: block;
        min-height: 100dvh;
    }

    .app-sidebar {
        position: fixed;
        top: 0;
        bottom: 0;
        inset-inline-start: 0;
        width: min(86vw, 300px);
        height: 100dvh;
        transform: translateX(100%);
        transition: transform .22s ease;
        z-index: 1040;
        box-shadow: none;
        padding-bottom: env(safe-area-inset-bottom);
    }

    [dir="ltr"] .app-sidebar {
        transform: translateX(-100%);
    }

    .app-sidebar.open,
    [dir="ltr"] .app-sidebar.open {
        transform: translateX(0);
        box-shadow: -12px 0 32px rgba(15, 23, 42, .28);
    }

    .sidebar-backdrop.show {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, .45);
        z-index: 1035;
    }

    body.sidebar-open {
        overflow: hidden;
        touch-action: none;
    }

    .app-topbar {
        padding: .45rem .7rem;
        gap: .4rem;
        flex-wrap: nowrap;
    }

    .app-topbar > .btn.d-lg-none {
        min-width: 42px;
        min-height: 42px;
        padding: .35rem;
        flex-shrink: 0;
    }

    .app-topbar form.d-flex {
        flex: 1 1 auto;
        min-width: 0;
    }

    .app-topbar form .form-select {
        width: 100% !important;
        max-width: 46vw;
        min-width: 0;
    }

    .app-topbar form > i {
        display: none;
    }

    .app-topbar .ms-auto {
        flex-shrink: 0;
        gap: .35rem !important;
    }

    .app-topbar .btn-sm.btn-primary {
        font-size: 0 !important;
        padding: .4rem .55rem;
        line-height: 1;
        min-width: 42px;
        min-height: 42px;
    }

    .app-topbar .btn-sm.btn-primary i {
        font-size: 1.05rem !important;
    }

    .app-topbar .dropdown-toggle {
        font-size: 0 !important;
        padding: .4rem .5rem;
        max-width: 46px;
        min-width: 42px;
        min-height: 42px;
    }

    .app-topbar .dropdown-toggle i,
    .app-topbar .dropdown-toggle::after {
        font-size: 1.05rem;
    }

    .app-topbar .dropdown-toggle .badge {
        display: none;
    }

    .app-content {
        padding: .8rem .7rem 1.4rem;
        padding-bottom: calc(1.4rem + env(safe-area-inset-bottom));
    }

    .page-head {
        align-items: stretch;
        margin-bottom: .85rem;
    }

    .page-head h1 {
        font-size: 1.12rem;
    }

    .page-head .subtitle {
        font-size: .8rem;
    }

    .page-head > .d-flex,
    .page-head .d-flex.gap-2 {
        width: 100%;
        flex-wrap: wrap;
    }

    .page-head .btn {
        flex: 1 1 auto;
        min-height: 42px;
        white-space: nowrap;
    }

    .stat-card {
        padding: .8rem .85rem;
    }

    .stat-card .stat-value {
        font-size: 1.15rem;
    }

    .nav-pills {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: .2rem;
        margin-bottom: .85rem !important;
    }

    .nav-pills .nav-link {
        white-space: nowrap;
        padding: .4rem .75rem;
    }

    .table-responsive {
        -webkit-overflow-scrolling: touch;
        margin-inline: -.15rem;
    }

    .card-header {
        padding: .65rem .8rem;
    }

    .pos-grid {
        grid-template-columns: 1fr;
        gap: .75rem;
    }

    .pos-total {
        font-size: 1.55rem;
    }

    .form-control,
    .form-select,
    input:not([type="hidden"]),
    select,
    textarea {
        font-size: 16px !important;
    }

    .btn {
        min-height: 42px;
    }

    .btn-sm {
        min-height: 36px;
    }

    .app-nav a {
        padding: .72rem 1.15rem;
        min-height: 44px;
        font-size: 1rem;
    }

    .login-wrap {
        padding: 1rem;
        align-items: start;
        padding-top: 7vh;
    }

    .login-card {
        padding: 1.4rem 1.15rem;
        border-radius: .85rem;
    }

    canvas {
        max-width: 100%;
    }

    .dropdown-menu {
        max-width: calc(100vw - 1.5rem);
    }
}

@media (max-width: 575.98px) {
    .app-content {
        padding: .65rem .55rem 1.2rem;
    }

    .page-head .btn {
        min-width: calc(50% - .4rem);
        font-size: .9rem;
    }

    .stat-card {
        gap: .65rem;
    }

    .stat-card .stat-icon {
        width: 40px;
        height: 40px;
        font-size: 1.15rem;
    }

    .input-group-lg > .form-control,
    .form-control-lg {
        min-height: 48px;
    }

    #cartTable input {
        min-width: 64px;
    }
}

@media (hover: none) {
    .app-nav a:hover {
        background: transparent;
        color: #cbd5e1;
    }

    .app-nav a.active:hover {
        background: rgba(20, 184, 166, .16);
        color: #fff;
    }
}
