/* =====================================================================
   myoffers.live — لوحة الإدارة
   ملف واحد منظّم بدل القواعد المكرّرة السابقة.
   ===================================================================== */

:root {
    --topbar-height: 122px;
}

body {
    background: var(--page);
    color: var(--ink);
    font-family: var(--font);
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;
}

:focus-visible {
    outline: 3px solid var(--brand);
    outline-offset: 2px;
}


/* ============================ الشاشات ============================ */

.screen {
    display: none;
    min-height: 100vh;
    min-height: 100dvh;
}

.screen.active {
    display: flex;
}

#loading-screen,
#login-screen {
    align-items: center;
    justify-content: center;
    padding: 24px;
}

#dashboard-screen {
    flex-direction: column;
}

/* ============================ بطاقة الدخول ============================ */

.card {
    background: var(--surface);
    width: 100%;
    max-width: 420px;
    padding: 32px;
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(17, 24, 39, 0.08);
    text-align: right;
}

.card h1 {
    font-size: 26px;
    margin-bottom: 24px;
    text-align: center;
    color: var(--ink);
}

.card p {
    text-align: center;
    color: var(--ink-muted);
}

.error {
    margin-top: 14px;
    color: var(--danger);
    text-align: center;
    min-height: 22px;
}

/* ============================ الحقول ============================ */

input,
select {
    width: 100%;
    padding: 14px;
    border: 1px solid #d0d5dd;
    border-radius: 10px;
    font-size: 16px;
    /* 16px يمنع تكبير الصفحة تلقائياً على iPhone */
    font-family: inherit;
    color: var(--ink);
    background: var(--surface);
    text-align: right;
}

.card input {
    margin-bottom: 16px;
}

input[readonly],
select:disabled {
    background: var(--surface-alt);
    color: var(--ink-soft);
    cursor: default;
}

.ltr-field {
    direction: ltr;
    text-align: left;
}

.password-group {
    position: relative;
}

.toggle-password {
    position: absolute;
    left: 8px;
    top: 50%;
    transform: translateY(-50%);
    width: auto;
    padding: 6px 10px;
    background: transparent;
    color: var(--ink-soft);
    font-size: 14px;
    font-weight: 600;
}

.toggle-password:hover {
    background: var(--surface-alt);
}

/* ============================ الأزرار ============================ */
button {
    width: 100%;
    padding: 14px;
    border: none;
    border-radius: 10px;
    background: var(--brand);
    color: #fff;
    font-size: 16px;
    font-weight: 700;
    font-family: inherit;
    cursor: pointer;
}

button:hover:not(:disabled) {
    background: var(--brand-dark);
}

button:disabled {
    background: #d1d5db;
    color: #6b7280;
    cursor: progress;
}

button.ghost {
    background: transparent;
    color: var(--ink-soft);
    border: 1px solid var(--line);
}

button.ghost:hover:not(:disabled) {
    background: var(--surface-alt);
    color: var(--ink);
}

.field-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.field-actions button {
    width: auto;
    padding: 10px 16px;
    font-size: 14px;
    background: var(--muted-bg);
    color: var(--ink);
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
}

.field-actions button:hover:not(:disabled) {
    background: var(--line);
}

.field-actions button.primary {
    background: var(--brand);
    color: #fff;
    border-color: var(--brand);
}

.field-actions button.primary:hover:not(:disabled) {
    background: var(--brand-dark);
}

.field-actions button.danger {
    background: #fee2e2;
    color: #991b1b;
    border-color: #fecaca;
}

.field-actions button.danger:hover:not(:disabled) {
    background: #fecaca;
}

/* ============================ الشريط العلوي ============================ */

.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    padding-top: env(safe-area-inset-top);
    position: sticky;
    top: 0;
    z-index: 100;
}

.topbar-inner {
    max-width: 1000px;
    margin: 0 auto;
    min-height: 64px;
    padding: 12px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.topbar h2 {
    font-size: 20px;
    color: var(--brand-text);
    font-weight: 800;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.topbar button {
    width: auto;
    padding: 10px 18px;
    font-size: 14px;
    flex-shrink: 0;
}

.topbar-nav {
    max-width: 1000px;
    margin: 0 auto;
    padding: 0 24px;
    display: flex;
    gap: 8px;
    border-top: 1px solid #f1f5f9;
}

.nav-tab {
    width: auto;
    background: transparent;
    color: var(--ink-muted);
    border: none;
    border-bottom: 3px solid transparent;
    border-radius: 0;
    padding: 14px 18px;
    font-size: 15px;
    font-weight: 700;
}

.nav-tab:hover:not(:disabled) {
    background: var(--surface-alt);
    color: var(--ink);
}

.nav-tab.active {
    color: var(--brand-text);
    border-bottom-color: var(--brand);
}

/* ============================ التخطيط ============================ */

.view {
    display: none;
}

.view.active {
    display: grid;
}

.content {
    width: 100%;
    max-width: 1000px;
    margin: 0 auto;
    padding: 32px 24px calc(32px + env(safe-area-inset-bottom));
    grid-template-columns: 320px 1fr;
    gap: 32px;
    align-items: start;
}

.content.single-column.active {
    grid-template-columns: minmax(0, 640px);
    justify-content: center;
}

/* ============================ الجداول ============================ */

table {
    width: 100%;
    max-width: 320px;
    table-layout: fixed;
    border-collapse: separate;
    border-spacing: 0;
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    /* overflow:hidden كان يكسر ثبات رأس الجدول — الحواف الآن على الخلايا */
}

thead th:first-child {
    border-top-right-radius: var(--radius);
}

thead th:last-child {
    border-top-left-radius: var(--radius);
}

tbody tr:last-child td:first-child {
    border-bottom-right-radius: var(--radius);
}

tbody tr:last-child td:last-child {
    border-bottom-left-radius: var(--radius);
}

th,
td {
    padding: 12px;
    border-bottom: 1px solid var(--line);
    text-align: right;
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
}

th {
    background: var(--surface-alt);
    color: var(--ink);
    font-weight: 700;
    position: sticky;
    top: var(--topbar-height);
    z-index: 10;
}

tbody tr:last-child td {
    border-bottom: none;
}

tbody tr[data-store]:hover {
    background: var(--surface-alt);
    cursor: pointer;
}

th:nth-child(1),
td:nth-child(1) {
    width: 96px;
}

th:nth-child(3),
td:nth-child(3) {
    width: 72px;
    text-align: center;
}

.table-empty td {
    color: var(--ink-muted);
    text-align: center;
    white-space: normal;
    padding: 24px 12px;
}

.status {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 13px;
    font-weight: 700;
}

.status.on {
    background: #dcfce7;
    color: #15803d;
}

.status.off {
    background: #f1f5f9;
    color: #64748b;
}

/* حقل حالة العرض للقراءة فقط */
#edit-active {
    font-weight: 700;
}

#edit-active[data-state="on"] {
    background: #dcfce7;
    color: #15803d;
    border-color: #bbf7d0;
}

#edit-active[data-state="off"] {
    background: #f1f5f9;
    color: #64748b;
}

.admin-search {
    display: block;
    width: 150px;
    flex-shrink: 0;
    padding: 8px 14px 8px 14px;
    border-radius: 20px;
    border: 1px solid var(--line);
    background: var(--surface-alt);
    font-size: 14px;
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="%236b7280" stroke-width="2"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>');
    background-repeat: no-repeat;
    background-position: left 10px center;
    background-size: 16px;
    padding-left: 34px;
}

.admin-search:focus {
    background-color: var(--surface);
    border-color: var(--brand);
}


@media (max-width: 900px) {
    .topbar h2 {
        font-size: 15px;
        max-width: 90px;
    }

    .admin-search {
        width: 90px;
        padding-left: 28px;
        font-size: 13px;
    }

    .topbar button {
        padding: 8px 12px;
        font-size: 13px;
    }
}


.admin-search[hidden] {
    display: none !important;
}

.field[hidden] {
    display: none !important;
}

/* لوحات فرعية داخل محرر المتجر — بلا التصاق أو تحديد ارتفاع خاص بـ .editor،
   حتى لا تتكدّس عدة صناديق قابلة للتمرير فوق بعضها */
.sub-panel {
    width: 100%;
    background: var(--surface-alt);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 16px;
    margin-top: 12px;
}

.sub-panel h3 {
    margin-bottom: 16px;
    font-size: 17px;
}

/* ============================ المحرّر ============================ */

.editor {
    width: 100%;
    background: var(--surface);
    border-radius: var(--radius);
    padding: 24px;
    box-shadow: var(--shadow);
    text-align: right;
    position: sticky;
    top: calc(var(--topbar-height) + 16px);
    max-height: calc(100vh - var(--topbar-height) - 48px);
    overflow-y: auto;
}

.editor h3 {
    margin-bottom: 20px;
    font-size: 22px;
    font-weight: 700;
    color: var(--brand-text);
    line-height: 1.3;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 22px;
}

.field>span {
    font-weight: 600;
    color: var(--ink-soft);
    font-size: 15px;
}

.field small,
.hint {
    color: var(--ink-muted);
    font-size: 13px;
    line-height: 1.5;
}

.locked-field {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#date-fields {
    display: grid;
    gap: 16px;
}

/* ============================ قائمة الصور ============================ */

#image-list {
    display: grid;
    gap: 10px;
    margin-top: 4px;
}

.image-item {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 12px;
    transition: background 0.15s ease, border-color 0.15s ease;
}

.image-item span {
    font-size: 14px;
    color: var(--ink);
    direction: ltr;
    text-align: left;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}

.image-item input[type="checkbox"] {
    width: 20px;
    height: 20px;
    margin: 0;
    padding: 0;
    flex-shrink: 0;
    accent-color: var(--brand);
    display: none;
}

.image-item.delete-mode input[type="checkbox"] {
    display: block;
}

.image-item.delete-mode {
    cursor: pointer;
}

.image-item.selected {
    background: var(--brand-soft);
    border-color: var(--brand);
}

/* ============================ رسالة الحالة ============================ */

.toast {
    position: fixed;
    bottom: calc(20px + env(safe-area-inset-bottom));
    left: 50%;
    transform: translate(-50%, 20px);
    max-width: min(92vw, 460px);
    padding: 14px 20px;
    border-radius: var(--radius);
    background: var(--ink);
    color: #fff;
    font-size: 15px;
    font-weight: 600;
    text-align: center;
    box-shadow: 0 8px 24px rgba(17, 24, 39, 0.25);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
    z-index: 1000;
}

.toast.visible {
    opacity: 1;
    transform: translate(-50%, 0);
}

.toast-success {
    background: var(--success);
}

.toast-error {
    background: var(--danger);
}

.toast-info {
    background: var(--warn);
}

/* ============================ الهاتف ============================ */

@media (max-width: 900px) {
    .content {
        padding: 20px 16px calc(24px + env(safe-area-inset-bottom));
        gap: 20px;
    }

    .view.active,
    .content.single-column.active {
        grid-template-columns: minmax(0, 1fr);
        justify-content: stretch;
    }

    .topbar-nav {
        padding: 0 8px;
        gap: 0;
    }

    .nav-tab {
        flex: 1;
        text-align: center;
        padding: 14px 8px;
        font-size: 14px;
    }

    table {
        max-width: 100%;
    }

    /* على الهاتف المحرّر يمتد طبيعياً بدل أن يصبح صندوقاً بشريط تمرير داخلي */
    .editor {
        position: static;
        max-height: none;
        overflow: visible;
        padding: 20px 16px;
    }

    th {
        position: static;
    }

    .field-actions button {
        flex: 1 1 140px;
    }
}


/* ============================ تبديل طريقة الدخول ============================ */

.login-method-switch {
    display: flex;
    gap: 8px;
    margin-bottom: 20px;
}

.method-btn {
    flex: 1;
    background: var(--surface-alt);
    color: var(--ink-soft);
    border: 1px solid var(--line);
    padding: 10px;
    font-size: 14px;
}

.method-btn.active {
    background: var(--brand);
    color: #fff;
    border-color: var(--brand);
}

#phone-step-number,
#phone-step-code {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#resend-code-btn {
    margin-top: 4px;
}

/* ============================ زر الدخول بـ Google ============================ */

.google-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    background: var(--surface);
    color: var(--ink);
    border: 1px solid #d0d5dd;
    margin-bottom: 4px;
}

.google-btn:hover:not(:disabled) {
    background: var(--surface-alt);
}

.login-divider {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 18px 0;
    color: var(--ink-muted);
    font-size: 13px;
}

.login-divider::before,
.login-divider::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--line);
}

/* ============================ المشرفون على المتجر (سوبر أدمن) ============================ */

.store-admin-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    margin-bottom: 8px;
    flex-wrap: wrap;
}

.store-admin-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.store-admin-uid,
.store-admin-stores,
.store-admin-contact {
    font-size: 12px;
    color: var(--ink-muted);
    direction: ltr;
    text-align: right;
    word-break: break-all;
}

/* ============================ تذييل لوحة الإدارة ============================ */

.admin-footer {
    background: var(--surface);
    border-top: 1px solid var(--line);
    margin-top: 32px;
    padding: 24px;
}

.admin-footer[hidden] {
    display: none !important;
}

.admin-footer-inner {
    max-width: 1000px;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
    gap: 24px;
    justify-content: space-between;
}

.admin-footer-section {
    flex: 1 1 220px;
    min-width: 200px;
}

.admin-footer-section h4 {
    font-size: 14px;
    color: var(--ink-soft);
    margin-bottom: 8px;
}

.admin-footer-section p {
    font-size: 13px;
    color: var(--ink-muted);
    line-height: 1.6;
    margin: 0 0 8px;
}

.admin-footer-logout {
    display: flex;
    align-items: flex-start;
    justify-content: flex-end;
}

.admin-footer-section a {
    display: inline-block;
    color: var(--brand-text);
    font-weight: 700;
    font-size: 13px;
    text-decoration: none;
}

.admin-footer-section a:hover {
    text-decoration: underline;
}


/* العنوان وحده في الشريط العلوي الآن — يظهر الاسم كاملاً على الهاتف */
@media (max-width: 900px) {
    .topbar h2 {
        max-width: none;
        white-space: normal;
        font-size: 17px;
        line-height: 1.4;
    }
}


/* زر "نسيت كلمة المرور؟" — رابط نصّي بسيط تحت زر الدخول */
button.link-btn {
    background: transparent;
    color: var(--brand-text);
    font-size: 14px;
    font-weight: 600;
    padding: 8px;
    margin-top: 8px;
}

button.link-btn:hover:not(:disabled) {
    background: transparent;
    text-decoration: underline;
}

.icon-btn {
    width: 36px;
    height: 36px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: var(--surface-alt);
    color: var(--ink-soft);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: none;
}

.icon-btn:hover:not(:disabled) {
    background: var(--surface-alt);
}


/* ============================ المشاهدات ============================ */

.views-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}

.views-tile {
    background: var(--surface-alt);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    padding: 10px 6px;
    text-align: center;
}

.views-tile strong {
    display: block;
    font-size: 22px;
    line-height: 1.2;
    color: var(--brand-text);
}

.views-tile span {
    font-size: 12px;
    color: var(--ink-muted);
}

.views-note {
    margin-top: 10px;
    font-size: 14px;
    line-height: 1.6;
    color: var(--ink-soft);
}

.views-note.muted {
    margin-top: 4px;
    font-size: 13px;
    color: var(--ink-muted);
}

@media (max-width: 480px) {
    .views-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

.views-record {
    margin-top: 12px;
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    background: var(--brand-soft);
    border: 1px solid var(--brand);
    color: var(--ink);
    font-size: 15px;
    line-height: 1.6;
}

.views-record.broken {
    background: #dcfce7;
    border-color: #15803d;
}

.views-record-note {
    margin-top: 4px;
    font-size: 13px;
    color: var(--ink-soft);
}

.views-tips {
    margin-top: 14px;
    padding: 12px 14px;
    border: 1px dashed var(--line-strong);
    border-radius: var(--radius-sm);
    font-size: 13px;
    line-height: 1.7;
    color: var(--ink-muted);
}

.views-tips strong {
    display: block;
    margin-bottom: 4px;
    color: var(--ink-soft);
}

.views-tips ul {
    margin: 0;
    padding-inline-start: 18px;
}

.views-record small {
    font-size: 13px;
    color: var(--ink-soft);
}

.views-top {
    margin-top: 12px;
    padding: 10px 14px;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    background: var(--surface-alt);
    font-size: 13px;
    color: var(--ink-muted);
}

.views-top div {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 18px;
    margin-top: 4px;
}

.views-top strong {
    font-size: 16px;
    color: var(--ink);
}