@import url("/css/tokens.css");

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
    margin: 0;
    font-family: var(--font);
    font-size: var(--text-md);
    line-height: 1.5;
    background: var(--page);
    color: var(--text);
}

a { color: var(--brand); }
h1, h2, h3 { margin: 0 0 var(--space-3); }
h1 { font-size: var(--text-xl); }
h2 { font-size: var(--text-lg); }

/* ---------- الهيكل ---------- */
.shell { display: flex; min-height: 100vh; }

.side {
    width: var(--sidebar-width);
    background: var(--ink);
    color: var(--on-dark);
    flex-shrink: 0;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    z-index: var(--layer-sidebar);
}
.side-brand {
    display: flex; align-items: center; gap: var(--space-2);
    padding: var(--space-4); border-bottom: 1px solid var(--ink-soft);
    font-weight: 800; font-size: var(--text-md); line-height: 1.3;
}
.side-brand img { width: 34px; height: 34px; }
.side-group { padding: var(--space-2) 0; }
.side-group-title { padding: var(--space-2) var(--space-4) var(--space-1); font-size: var(--text-xs); color: var(--on-dark-muted); }
.side a {
    display: block; padding: 8px var(--space-4); color: var(--on-dark); text-decoration: none; font-size: var(--text-md);
    border-inline-start: 3px solid transparent;
}
.side a:hover { background: var(--ink-soft); }
.side a.current { background: var(--ink-soft); border-inline-start-color: var(--accent); font-weight: 700; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.top {
    height: var(--topbar-height); background: var(--surface); border-bottom: 1px solid var(--border);
    display: flex; align-items: center; gap: var(--space-3); padding: 0 var(--space-5);
    position: sticky; top: 0; z-index: var(--layer-bar);
}
.top-title { font-size: var(--text-lg); font-weight: 700; flex: 1; }
.top-user { color: var(--muted); font-size: var(--text-sm); }
.menu-btn { display: none; }

.content { padding: var(--space-5); max-width: 1320px; width: 100%; margin: 0 auto; }

.tabbar { display: none; }

/* ---------- المكونات ---------- */
.card {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: var(--space-4) var(--space-5); margin-bottom: var(--space-4); box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); margin-bottom: var(--space-3); flex-wrap: wrap; }
.card-head h2 { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: var(--text-sm); }

.btn {
    font: inherit; height: var(--control-height); padding: 0 16px; border-radius: var(--radius-sm);
    border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); cursor: pointer;
    display: inline-flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap; text-decoration: none;
}
.btn:hover { border-color: var(--brand); color: var(--brand); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); color: #fff; }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-small { height: 30px; padding: 0 10px; font-size: var(--text-sm); }
.btn svg { width: 18px; height: 18px; flex-shrink: 0; }
.btn-link { border: 0; background: none; color: var(--brand); padding: 0 4px; height: auto; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: var(--space-3); }
.field label { font-size: var(--text-sm); color: var(--muted); font-weight: 700; }
.field .hint { font-size: var(--text-xs); color: var(--muted); }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=search], select, textarea {
    font: inherit; height: var(--control-height); padding: 0 12px; border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm); background: var(--surface); color: var(--text); width: 100%;
}
textarea { height: auto; min-height: 80px; padding: 8px 12px; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--brand); box-shadow: var(--focus-ring); }
.row { display: flex; gap: var(--space-3); flex-wrap: wrap; align-items: flex-end; }
.row > .field { flex: 1; min-width: 160px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); }
table { width: 100%; border-collapse: collapse; background: var(--surface); }
th, td { padding: var(--cell-padding); text-align: start; border-bottom: 1px solid var(--border); vertical-align: top; }
th { background: var(--surface-alt); font-size: var(--text-sm); color: var(--muted); font-weight: 700; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: end; font-variant-numeric: tabular-nums; }

.badge { display: inline-block; white-space: nowrap; margin: 1px 0; padding: 1px 8px; border-radius: 20px; font-size: var(--text-xs); font-weight: 700; background: var(--surface-alt); border: 1px solid var(--border); }
.badge-ok { background: var(--success-bg); color: var(--success); border-color: transparent; }
.badge-off { background: var(--danger-bg); color: var(--danger); border-color: transparent; }
.badge-warn { background: var(--warning-bg); color: var(--warning-text); border-color: transparent; }

.alert { padding: var(--space-3) var(--space-4); border-radius: var(--radius-sm); margin-bottom: var(--space-3); }
.alert-error { background: var(--danger-bg); color: var(--danger); }
.alert-ok { background: var(--success-bg); color: var(--success); }
.alert-warn { background: var(--warning-bg); color: var(--warning-text); }
.alert-info { background: var(--info-bg); color: var(--info); }

.toast {
    position: fixed; bottom: 24px; inset-inline-start: 50%; transform: translateX(50%);
    background: var(--ink); color: var(--on-dark); padding: 10px 18px; border-radius: var(--radius);
    box-shadow: var(--shadow-raised); z-index: var(--layer-toast); max-width: 90vw;
}
.toast.error { background: var(--danger); color: #fff; }

.dialog-backdrop { position: fixed; inset: 0; background: rgba(23, 33, 43, 0.45); z-index: var(--layer-menu); display: flex; align-items: center; justify-content: center; padding: var(--space-4); }
.dialog { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-raised); width: 100%; max-width: 480px; max-height: 90vh; overflow: auto; padding: var(--space-5); }
.dialog h2 { margin-bottom: var(--space-4); }
.dialog-actions { display: flex; gap: var(--space-2); justify-content: flex-end; margin-top: var(--space-4); }

.secret { font-family: Consolas, monospace; font-size: var(--text-lg); background: var(--warning-bg); padding: 8px 12px; border-radius: var(--radius-sm); direction: ltr; text-align: center; user-select: all; letter-spacing: 1px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: var(--space-3); }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: var(--space-4); }
.stat .label { color: var(--muted); font-size: var(--text-sm); }
.stat .value { font-size: var(--text-xl); font-weight: 800; }

.perm-group { margin-bottom: var(--space-4); }
.perm-group h3 { font-size: var(--text-md); color: var(--brand); margin-bottom: var(--space-2); }
.perm { display: flex; gap: var(--space-2); align-items: center; padding: 4px 0; }
.perm input { width: 18px; height: 18px; accent-color: var(--brand); }

.pager { display: flex; gap: var(--space-2); align-items: center; justify-content: center; margin-top: var(--space-3); }
.details { font-size: var(--text-xs); color: var(--muted); direction: rtl; white-space: pre-wrap; word-break: break-word; }

/* ---------- اختيار مادة ---------- */
.picker { position: relative; width: 100%; }
.picker-list {
    position: absolute; top: calc(100% + 2px); inset-inline: 0; background: var(--surface); border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm); box-shadow: var(--shadow-raised); z-index: var(--layer-menu); max-height: 320px; overflow-y: auto;
}
.picker-item { display: flex; gap: var(--space-3); align-items: center; width: 100%; text-align: start; padding: 8px 12px; border: 0; background: none; font: inherit; cursor: pointer; border-bottom: 1px solid var(--border); }
.picker-item:last-child { border-bottom: 0; }
.picker-item.active, .picker-item:hover { background: var(--brand-soft); }
.picker-item span:nth-child(2) { flex: 1; }
.picker-code { font-weight: 800; color: var(--brand); min-width: 44px; font-variant-numeric: tabular-nums; }
.picker-empty { padding: 10px 12px; }

.code { font-weight: 800; color: var(--brand); font-variant-numeric: tabular-nums; }
input.qty-input { width: 90px; text-align: center; }
.lines td { vertical-align: middle; }
.filters { display: flex; gap: var(--space-3); flex-wrap: wrap; align-items: flex-end; margin-bottom: var(--space-3); }
.filters .field { margin-bottom: 0; min-width: 150px; }
.filters .grow { flex: 1; min-width: 220px; }
.check-field { display: flex; align-items: center; gap: 6px; height: var(--control-height); }
.check-field input { width: 18px; height: 18px; accent-color: var(--brand); }
.thumb { width: 44px; height: 44px; object-fit: cover; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface-alt); }
.photo-preview { max-width: 100%; max-height: 220px; border-radius: var(--radius-sm); border: 1px solid var(--border); }
.split { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-4); }
@media (max-width: 900px) { .split { grid-template-columns: 1fr; } }
.row-low td { background: var(--warning-bg); }
.dialog.wide { max-width: 760px; }
.status-sent { background: var(--info-bg); color: var(--info); border-color: transparent; }
.status-received, .status-approved { background: var(--success-bg); color: var(--success); border-color: transparent; }
.status-cancelled { background: var(--danger-bg); color: var(--danger); border-color: transparent; }
.status-draft { background: var(--warning-bg); color: var(--warning-text); border-color: transparent; }
.diff-plus { color: var(--success); font-weight: 700; direction: ltr; unicode-bidi: plaintext; }
.diff-minus { color: var(--danger); font-weight: 700; direction: ltr; unicode-bidi: plaintext; }

/* ---------- المستند المطبوع ---------- */
.doc { background: #fff; max-width: 800px; margin: 0 auto; padding: var(--space-5); border: 1px solid var(--border); }
.doc-head { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 2px solid var(--ink); padding-bottom: var(--space-3); margin-bottom: var(--space-4); gap: var(--space-4); }
.doc-head h1 { margin: 0; font-size: var(--text-xl); }
.doc-meta { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px var(--space-4); margin-bottom: var(--space-4); }
.doc-sign { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--space-5); margin-top: var(--space-6); text-align: center; }
.doc-sign div { border-top: 1px solid var(--ink); padding-top: 6px; }
@media print { .doc { border: 0; padding: 0; max-width: none; } }

/* ---------- صفحة الدخول ---------- */
.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: var(--space-4); background: linear-gradient(160deg, var(--ink), var(--brand-dark)); }
.login-box { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-raised); width: 100%; max-width: 380px; padding: var(--space-6) var(--space-5); }
.login-box .logo { text-align: center; margin-bottom: var(--space-5); }
.login-box .logo img { width: 64px; height: 64px; }
.login-box .logo h1 { font-size: var(--text-lg); margin: var(--space-2) 0 0; line-height: 1.4; }
.login-box .btn { width: 100%; height: 44px; margin-top: var(--space-2); }

/* ---------- الموبايل ---------- */
@media (max-width: 900px) {
    .side { position: fixed; inset-block: 0; inset-inline-start: 0; transform: translateX(100%); transition: transform 0.2s; box-shadow: var(--shadow-raised); }
    .side.open { transform: none; }
    .menu-btn { display: inline-flex; }
    .top { padding: 0 var(--space-3); }
    .top-user { display: none; }
    .content { padding: var(--space-3); padding-bottom: calc(var(--tabbar-height) + var(--space-4)); }
    .card { padding: var(--space-3); }
    .tabbar {
        display: flex; position: fixed; bottom: 0; inset-inline: 0; height: var(--tabbar-height); background: var(--surface);
        border-top: 1px solid var(--border); z-index: var(--layer-bar);
    }
    .tabbar a, .tabbar button {
        flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
        font: inherit; font-size: var(--text-xs); color: var(--muted); text-decoration: none; background: none; border: 0;
    }
    .tabbar .current { color: var(--brand); font-weight: 700; }
    .tabbar svg { width: 22px; height: 22px; }
}

@media print {
    .side, .top, .tabbar, .noprint { display: none !important; }
    body { background: #fff; }
    .content { padding: 0; }
    .card { box-shadow: none; border: 0; }
}

/* ---------- المرحلة 3 ---------- */
.doc-bar { display: flex; gap: var(--space-2); flex-wrap: wrap; justify-content: center; padding: var(--space-3); }
.box-card { display: flex; flex-direction: column; gap: var(--space-2); }
.box-card .actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: auto; }
.money-in { color: var(--success); }
.money-out { color: var(--danger); }
.totals-line { display: flex; justify-content: space-between; padding: 4px 0; border-bottom: 1px dashed var(--border); }
.totals-line.big { font-size: var(--text-lg); font-weight: 800; border-bottom: 0; }
input.price-input { width: 110px; }
.reversed td { text-decoration: line-through; color: var(--muted); }
.reversed td:last-child { text-decoration: none; }

/* ---------- المرحلة 4 ---------- */
.cust-card { display: flex; gap: var(--space-4); align-items: center; flex-wrap: wrap; background: var(--brand-soft); border-radius: var(--radius-sm); padding: var(--space-2) var(--space-3); margin-bottom: var(--space-3); }
.cust-card strong { font-size: var(--text-lg); }
input.price-cell { width: 120px; }
select.wh-cell { width: auto; min-width: 110px; }
.inv-total { font-size: var(--text-2xl); }
.change { color: var(--success); font-weight: 800; }
.invoice-foot { text-align: center; margin-top: var(--space-4); color: var(--muted); }

/* ---------- المرحلة 5 ---------- */
.hours { display: flex; align-items: flex-end; gap: 4px; height: 140px; padding-top: var(--space-3); }
.hours .bar { flex: 1; background: var(--brand); border-radius: 3px 3px 0 0; min-height: 2px; position: relative; }
.hours .bar span { position: absolute; bottom: -20px; inset-inline: 0; text-align: center; font-size: 10px; color: var(--muted); }
.attn { padding: 8px 0; border-bottom: 1px dashed var(--border); display: flex; justify-content: space-between; gap: var(--space-2); }
.attn:last-child { border-bottom: 0; }
.feed { padding: 6px 0; border-bottom: 1px solid var(--border); font-size: var(--text-sm); }
.feed .who { font-weight: 700; }
.h-1 { height: 4%; } .h-2 { height: 8%; }
