* { box-sizing: border-box; }
html, body { max-width: 100%; overflow-x: hidden; }
body {
    font-family: 'Segoe UI', Tahoma, Arial, sans-serif;
    background: #f4f6f9;
    margin: 0;
    color: #222;
}
.topbar {
    background: #1f2d3d;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    flex-wrap: wrap;
    gap: 8px;
}
.brand { font-weight: bold; font-size: 20px; word-break: break-word; }
.nav-toggle { display: none; background: none; border: none; color: #fff; font-size: 22px; cursor: pointer; padding: 4px 8px; line-height: 1; }
.nav-close { display: none; }
.mainnav {
    display: flex; flex-wrap: wrap;
    background: #24334a;
    padding: 6px 16px;
}
.mainnav a {
    color: #cfd8e3;
    text-decoration: none;
    margin: 0 8px;
    padding: 6px 10px;
    border-radius: 4px;
    white-space: nowrap;
}
.mainnav a:hover { background: #34495e; color: #fff; }
.userbox { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-width: 0; }
.userbox span { word-break: break-word; min-width: 0; }
.btn-logout {
    background: #e74c3c; color: #fff; padding: 6px 12px; border-radius: 4px; text-decoration: none;
}
.nav-overlay { display: none; }
.container { max-width: 1200px; margin: 20px auto; padding: 0 15px; }
.card { background: #fff; border-radius: 8px; padding: 20px; box-shadow: 0 1px 3px rgba(0,0,0,.1); margin-bottom: 20px; overflow-x: auto; }
table { width: 100%; min-width: 560px; border-collapse: collapse; background: #fff; }
table th, table td { padding: 10px; border-bottom: 1px solid #e5e5e5; text-align: right; white-space: nowrap; }
table th { background: #f0f2f5; }
tr:hover { background: #f9fafc; }
.btn { display: inline-block; padding: 8px 16px; border-radius: 5px; border: none; cursor: pointer; text-decoration: none; font-size: 14px; }
.btn-primary { background: #2980b9; color: #fff; }
.btn-success { background: #27ae60; color: #fff; }
.btn-danger { background: #e74c3c; color: #fff; }
.btn-secondary { background: #95a5a6; color: #fff; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
input, select, textarea {
    width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px; margin-bottom: 12px; font-family: inherit;
}
label { font-weight: bold; display: block; margin-bottom: 4px; }
.form-row { display: flex; gap: 15px; flex-wrap: wrap; }
.form-row > div { flex: 1; min-width: 200px; }
.grid-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; margin-bottom: 20px; }
.stat-box { background: #fff; border-radius: 8px; padding: 18px; box-shadow: 0 1px 3px rgba(0,0,0,.1); text-align: center; }
.stat-box .num { font-size: 26px; font-weight: bold; color: #2980b9; }
.stat-box .lbl { color: #777; margin-top: 5px; }
.alert { padding: 12px; border-radius: 5px; margin-bottom: 15px; }
.alert-error { background: #fdecea; color: #c0392b; border: 1px solid #f5b7b1; }
.alert-success { background: #eafaf1; color: #1e8449; border: 1px solid #a9dfbf; }
.alert-warn { background: #fef9e7; color: #9a7d0a; border: 1px solid #f7dc6f; }
.badge { padding: 3px 8px; border-radius: 10px; font-size: 12px; }
.badge-low { background: #fdecea; color: #c0392b; }
.badge-ok { background: #eafaf1; color: #1e8449; }
.login-wrap { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-box { background: #fff; padding: 35px; border-radius: 10px; width: 350px; box-shadow: 0 2px 10px rgba(0,0,0,.15); }
.login-box h2 { text-align: center; margin-bottom: 20px; }
.profit-pos { color: #1e8449; font-weight: bold; }
.profit-neg { color: #c0392b; font-weight: bold; }
.search-box { margin-bottom: 15px; }
.pricing-toggle { display: flex; gap: 15px; margin-bottom: 15px; }
.item-row { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; }
.item-row > * { flex: 1; }
.remove-item { flex: 0 0 auto; }

/* ===================== شكل الموبايل ===================== */
@media (max-width: 768px) {
    .container { padding: 0 10px; margin: 12px auto; }
    .card { padding: 14px; border-radius: 6px; }

    .topbar {
        padding: 10px 12px;
        justify-content: space-between;
    }
    .brand { font-size: 16px; }
    .nav-toggle { display: block; order: -1; }
    .userbox { font-size: 13px; gap: 6px; }
    .userbox span { display: none; }

    /* قائمة جانبية بتفتح من اليمين */
    .nav-overlay {
        display: block;
        position: fixed; inset: 0;
        background: rgba(0,0,0,.45);
        opacity: 0; pointer-events: none;
        transition: opacity .2s ease;
        z-index: 90;
    }
    .nav-overlay.show { opacity: 1; pointer-events: auto; }

    .mainnav {
        position: fixed;
        top: 0; right: 0; bottom: 0;
        width: 78%; max-width: 300px;
        flex-direction: column;
        align-items: stretch;
        background: #1f2d3d;
        padding: 55px 16px 20px;
        gap: 2px;
        transform: translateX(100%);
        transition: transform .25s ease;
        z-index: 95;
        overflow-y: auto;
        box-shadow: -4px 0 15px rgba(0,0,0,.2);
    }
    .mainnav.open { transform: translateX(0); }
    .mainnav a { margin: 0; padding: 12px 10px; font-size: 15px; border-bottom: 1px solid rgba(255,255,255,.08); }
    .nav-close {
        display: block;
        position: absolute; top: 10px; left: 14px;
        background: none; border: none; color: #fff; font-size: 20px; cursor: pointer;
    }

    h1, h2 { font-size: 19px; }
    h3 { font-size: 16px; }

    /* أي صف فيه أكتر من عنصر ياخد العرض كله ويترص تحت بعضه */
    .form-row { flex-direction: column; gap: 0; }
    .form-row > div { min-width: 100%; }

    .item-row {
        flex-direction: column;
        align-items: stretch;
        gap: 6px;
        padding: 10px;
        background: #f7f8fa;
        border-radius: 6px;
        margin-bottom: 12px;
    }
    .item-row input, .item-row select { margin-bottom: 0; }
    .item-row .remove-item { width: 100%; }

    .btn { padding: 10px 14px; font-size: 14px; }
    .btn-sm { padding: 7px 10px; font-size: 12.5px; }

    /* أي مجموعة أزرار جنب بعض في صف واحد بتترص وتاخد العرض كامل */
    div[style*="display:flex"], div[style*="display: flex"] {
        flex-wrap: wrap !important;
    }

    .grid-stats { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
    .stat-box { padding: 12px; }
    .stat-box .num { font-size: 20px; }

    .login-box { width: 92vw; max-width: 350px; padding: 24px; }

    table { min-width: 480px; font-size: 13px; }
    table th, table td { padding: 7px; }
}

@media (max-width: 420px) {
    table { min-width: 420px; font-size: 12px; }
}
