*, *::before, *::after { box-sizing: border-box; }

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    margin: 0;
    font-family: Tahoma, "Vazirmatn", sans-serif;
    font-size: 16px;
    line-height: 1.5;
    color: #212529;
    background: #f6f7fb;
    overflow-x: hidden;
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    touch-action: manipulation;
}

img { max-width: 100%; height: auto; }
a { color: #0d6efd; }
h1, h2, h3, h4, h5, h6 { margin: 0 0 .5rem; font-weight: 700; line-height: 1.3; }
p { margin: 0 0 1rem; }
button, input, select { font-family: inherit; }

/* ----- layout ----- */
.app-shell {
    width: 100%;
    max-width: 1400px;
    margin: 0 auto;
    padding: 12px 12px calc(20px + env(safe-area-inset-bottom));
}
.app-header {
    display: flex;
    flex-wrap: wrap;
    gap: 10px 16px;
    justify-content: space-between;
    align-items: center;
}
.app-title { font-size: 1.35rem; }
.app-nav { display: flex; flex-wrap: wrap; gap: 8px; }
.nav-short { display: none; }
.title-full { display: none; }
.title-short { display: inline; }

.row {
    display: flex;
    flex-wrap: wrap;
    margin-left: -6px;
    margin-right: -6px;
}
.row > * { padding-left: 6px; padding-right: 6px; min-width: 0; }
.g-2 { row-gap: 8px; }
.g-3 { row-gap: 12px; }
.col, .col-6, .col-12, .col-md-4, .col-md-auto { flex: 0 0 auto; }
.col { width: 50%; }
.col-6 { width: 50%; }
.col-12 { width: 100%; }
.col-md-4 { width: 100%; }

/* ----- forms / buttons ----- */
.form-label {
    display: block;
    margin-bottom: .25rem;
    font-weight: 600;
}
.form-control, .form-select {
    display: block;
    width: 100%;
    padding: .5rem .75rem;
    font-size: 16px;
    line-height: 1.4;
    color: #212529;
    background: #fff;
    border: 1px solid #ced4da;
    border-radius: 8px;
    min-width: 0;
}
.form-check {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 6px 0;
    font-weight: 600;
}
textarea.form-control,
.sms-text {
    display: block;
    width: 100%;
    min-height: 110px;
    padding: .5rem .75rem;
    font-size: 16px;
    line-height: 1.5;
    color: #212529;
    background: #fff;
    border: 1px solid #ced4da;
    border-radius: 8px;
    resize: vertical;
    font-family: inherit;
}
.sms-rec-table td:last-child,
.sms-log-table td { white-space: nowrap; }
code { font-size: .9em; background: #f1f5f9; padding: 1px 5px; border-radius: 4px; }
.form-control-sm { padding: .25rem .5rem; width: auto; }
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .25rem;
    padding: .5rem .9rem;
    font-size: 15px;
    font-weight: 600;
    line-height: 1.3;
    text-decoration: none;
    border: 1px solid transparent;
    border-radius: 8px;
    cursor: pointer;
    background: #e9ecef;
    color: #212529;
    min-height: 40px;
}
.btn-sm { padding: .35rem .65rem; min-height: 36px; font-size: 14px; }
.btn-primary { background: #0d6efd; border-color: #0d6efd; color: #fff; }
.btn-success { background: #198754; border-color: #198754; color: #fff; }
.btn-info { background: #0dcaf0; border-color: #0dcaf0; color: #055160; }
.btn-outline-primary { background: #fff; border-color: #0d6efd; color: #0d6efd; }
.btn-outline-secondary { background: #fff; border-color: #6c757d; color: #6c757d; }
.btn-outline-danger { background: #fff; border-color: #dc3545; color: #dc3545; }
.btn.disabled, .btn:disabled { opacity: .65; pointer-events: none; }
.w-100 { width: 100%; }

/* ----- surfaces ----- */
.alert {
    padding: 12px 14px;
    border-radius: 10px;
    margin-bottom: 12px;
}
.alert-success { background: #d1e7dd; color: #0f5132; }
.alert-warning { background: #fff3cd; color: #664d03; }
.alert-danger { background: #f8d7da; color: #842029; }
.alert-info { background: #cff4fc; color: #055160; }
.card {
    background: #fff;
    border: 1px solid #dee2e6;
    border-radius: 12px;
    overflow: hidden;
}
.card-body { padding: 16px; }
.card-title { font-size: 1.1rem; }
.card-img-top { width: 100%; display: block; object-fit: cover; }
.border { border: 1px solid #dee2e6; }
.rounded { border-radius: 12px; }
.progress { background: #e9ecef; border-radius: 6px; overflow: hidden; }
.progress-bar { height: 100%; background: #0d6efd; }

.badge {
    display: inline-block;
    padding: .28em .55em;
    font-size: .82em;
    font-weight: 700;
    border-radius: 6px;
    line-height: 1.2;
}
.bg-success { background: #198754; color: #fff; }
.bg-secondary { background: #6c757d; color: #fff; }
.bg-info { background: #0dcaf0; }
.text-dark { color: #212529; }
.text-muted { color: #6c757d; }
.text-decoration-none { text-decoration: none; }
.small, small { font-size: .875em; }
.fw-bold { font-weight: 700; }
.fs-4 { font-size: 1.6rem; font-weight: 700; }
.h-100 { height: 100%; }

/* ----- table ----- */
.table-responsive { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
}
.table th, .table td {
    vertical-align: middle;
    padding: .45rem .55rem;
    border: 1px solid #dee2e6;
}
.table-light th { background: #f8f9fa; }
.yn { display: inline-block; font-size: 12px; font-weight: 700; }
.yn-yes { color: #9a3412; }
.yn-no { color: #64748b; }

@media (min-width: 768px) {
    .events-wrap {
        max-height: calc(100vh - 250px);
        overflow: auto;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        background: #fff;
        overflow-anchor: none;
    }
    .events-table {
        table-layout: fixed;
        width: 100%;
        min-width: 1040px;
        margin-bottom: 0;
    }
    .events-table col.col-num { width: 3.4%; }
    .events-table col.col-date { width: 7%; }
    .events-table col.col-time { width: 5.8%; }
    .events-table col.col-cam { width: 4.2%; }
    .events-table col.col-status { width: 7%; }
    .events-table col.col-name { width: 9.5%; }
    .events-table col.col-actions { width: 92px; }
    .events-table col.col-sim { width: 4.8%; }
    .events-table col.col-age { width: 6%; }
    .events-table col.col-sex { width: 4.8%; }
    .events-table col.col-yn { width: 4.2%; }
    .events-table col.col-photo { width: 7.2%; }
    .events-table thead th {
        position: sticky;
        top: 0;
        z-index: 4;
        background: #f8f9fa;
        white-space: normal;
        overflow-wrap: anywhere;
        word-break: break-word;
        line-height: 1.2;
        font-size: 12px;
        font-weight: 700;
        padding: 6px 4px;
        vertical-align: bottom;
        text-align: center;
        hyphens: auto;
    }
    .events-table td {
        padding: 4px;
        font-size: 12px;
        text-align: center;
        overflow: hidden;
        vertical-align: middle;
    }
    .events-table td.cell-name { text-align: start; overflow: visible; }
    .events-table td.cell-actions {
        width: 92px;
        max-width: 92px;
        overflow: hidden;
        white-space: nowrap;
        padding: 2px 1px;
    }
    .events-table td.cell-actions .row-actions {
        display: inline-flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        gap: 1px;
    }
    .events-table td.cell-actions .name-action {
        min-width: 26px;
        width: 26px;
        max-width: 26px;
        min-height: 26px;
        height: 26px;
        padding: 0;
        font-size: 12px;
        line-height: 1;
        flex: 0 0 26px;
        box-sizing: border-box;
    }
    .events-table td.cell-rownum { font-weight: 700; color: #64748b; }
    .events-table td.cell-age { overflow: visible; line-height: 1.2; }
    .events-table .thumb { max-width: 56px; max-height: 56px; }
    .event-child { box-shadow: none; border-inline-start: 4px solid #cbd5e1; }
}
.thumb {
    max-width: 70px;
    max-height: 70px;
    border-radius: 4px;
    border: 1px solid #ddd;
    object-fit: cover;
}
.zoomable { cursor: zoom-in; }
.cell-compact { display: none !important; }
.repeat-toggle {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    border: 1px solid #cbd5e1;
    background: #f8fafc;
    border-radius: 999px;
    padding: 2px 8px;
    font-size: 12px;
    font-weight: 700;
    color: #334155;
    cursor: pointer;
    line-height: 1.2;
}
.repeat-toggle .tri {
    display: inline-block;
    transition: transform .15s ease;
    font-size: 14px;
}
.repeat-toggle[aria-expanded="true"] .tri { transform: rotate(90deg); }
.event-child { background: #f8fafc; }
.event-child td { color: #475569; }
.events-table tr[hidden] { display: none !important; }
.row-no { font-weight: 800; color: #334155; }
.stats-line { flex: 1 1 220px; line-height: 1.7; }
.stats-line strong { color: #0f172a; }
body.lb-open { overflow: hidden; }
.img-lb {
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: rgba(8, 10, 16, .92);
    display: flex;
    flex-direction: column;
}
.img-lb[hidden] { display: none !important; }
.img-lb-toolbar {
    display: flex;
    justify-content: center;
    gap: 8px;
    padding: 10px;
    padding-top: calc(10px + env(safe-area-inset-top));
}
.img-lb-btn {
    min-width: 44px;
    min-height: 44px;
    border: 0;
    border-radius: 10px;
    background: #fff;
    color: #111;
    font-size: 22px;
    font-weight: 700;
    cursor: pointer;
}
.img-lb-stage {
    flex: 1;
    overflow: hidden;
    touch-action: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
}
.img-lb-stage img {
    max-width: 100%;
    max-height: 100%;
    transform-origin: center center;
    user-select: none;
    pointer-events: none;
}
.img-lb-hint {
    color: #cbd5e1;
    text-align: center;
    font-size: 12px;
    padding: 8px 12px calc(12px + env(safe-area-inset-bottom));
}
.identity-photo { width: 100%; height: 140px; object-fit: cover; background: #f3f3f3; }
.name-action { line-height: 1; padding: .15rem .4rem; min-width: 36px; min-height: 36px; }
.row-actions {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    flex-wrap: nowrap;
}
.row-actions form { display: inline; margin: 0; }
.rename-dialog {
    border: none;
    border-radius: 12px;
    padding: 0;
    width: min(420px, calc(100vw - 24px));
    box-shadow: 0 12px 40px rgba(0,0,0,.25);
}
.rename-dialog::backdrop { background: rgba(0,0,0,.45); }

/* ----- helpers ----- */
.d-flex { display: flex; }
.d-inline { display: inline; }
.flex-wrap { flex-wrap: wrap; }
.align-items-center { align-items: center; }
.align-items-end { align-items: flex-end; }
.align-self-center { align-self: center; }
.justify-content-between { justify-content: space-between; }
.justify-content-center { justify-content: center; }
.justify-content-end { justify-content: flex-end; }
.gap-1 { gap: .25rem; }
.gap-2 { gap: .5rem; }
.mb-0 { margin-bottom: 0; }
.mb-2 { margin-bottom: .5rem; }
.mb-3 { margin-bottom: 1rem; }
.mb-4 { margin-bottom: 1.5rem; }
.mt-1 { margin-top: .25rem; }
.mt-2 { margin-top: .5rem; }
.mt-3 { margin-top: 1rem; }
.my-3 { margin-top: 1rem; margin-bottom: 1rem; }
.p-2 { padding: .5rem; }
.p-3 { padding: 1rem; }
.text-center { text-align: center; }
.d-none { display: none !important; }
.d-md-none { display: inline; }
.filter-bar .form-control,
.filter-bar .form-select { min-width: 0; }
.export-bar { align-items: center; }
.pager { flex-wrap: wrap; }
.filter-actions { display: flex; gap: 8px; }

@media (min-width: 576px) {
    .nav-short { display: none; }
    .nav-full { display: inline; }
    .d-sm-none { display: none !important; }
    .d-sm-inline { display: inline !important; }
    .col { width: 50%; }
}

@media (min-width: 768px) {
    .app-shell { padding: 16px 20px 24px; }
    .d-md-none { display: none !important; }
    .d-md-inline { display: inline !important; }
    .title-full { display: inline; }
    .title-short { display: none; }
    .col-md-auto { width: auto; }
    .col-md-4 { width: 33.333%; }
    .col { width: 25%; }
    .filter-bar .form-control,
    .filter-bar .form-select { min-width: 150px; }
    .form-control, .form-select { font-size: 14px; }
}

@media (min-width: 992px) {
    .col { width: 16.666%; }
}

@media (max-width: 767.98px) {
    .app-header { align-items: stretch; }
    .app-title { font-size: 1.15rem; }
    .nav-full { display: none; }
    .nav-short { display: inline; }
    .title-full { display: none; }
    .title-short { display: inline; }
    .app-nav .btn {
        flex: 1 1 0;
        min-height: 44px;
        font-size: 14px;
    }
    .btn { min-height: 44px; }
    input[type="file"] { min-height: 44px; }
    .filter-actions .btn { flex: 1; min-height: 44px; }
    .export-bar { flex-direction: column; align-items: stretch; }
    .export-bar .btn { width: 100%; min-height: 44px; }
    .name-action { min-width: 44px; min-height: 44px; }
    .thumb { max-width: 96px; max-height: 96px; }
    .identity-photo, .card-img-top { height: 160px; }
    .rename-dialog { width: calc(100% - 24px); }
    .rename-dialog .btn { min-height: 44px; flex: 1; }
    .rename-actions { flex-wrap: wrap; }
    .table-responsive { overflow: visible; }
    .events-table thead { display: none; }
    .events-table,
    .events-table tbody { display: block; width: 100%; }
    .events-table { border: none; background: transparent; table-layout: auto; min-width: 0; }
    .events-table tr.event-row {
        display: flex;
        flex-direction: column;
        background: #fff;
        border: 1px solid #dee2e6;
        border-radius: 12px;
        margin-bottom: 12px;
        padding: 10px 12px;
        box-shadow: 0 1px 2px rgba(0,0,0,.04);
        width: 100%;
        gap: 8px;
    }
    .events-table tr.event-child {
        margin-right: 12px;
        margin-top: -6px;
        background: #f8fafc;
        border-style: dashed;
    }
    .events-table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 12px;
        border: none;
        padding: 0;
        text-align: start;
        width: auto;
    }
    .events-table td::before { content: none; display: none; }
    .cell-desk { display: none !important; }
    .cell-compact {
        display: flex !important;
        width: 100%;
        justify-content: space-between;
        align-items: center;
        gap: 6px;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .cell-compact-meta span,
    .cell-compact-attrs span {
        flex: 0 0 auto;
        font-size: 12px;
        white-space: nowrap;
    }
    .cell-compact-attrs em {
        font-style: normal;
        color: #6c757d;
        font-weight: 700;
        margin-left: 2px;
    }
    .row-no {
        font-size: 12px;
        min-width: 2.2em;
    }
    .cell-compact-photos {
        justify-content: space-around;
        gap: 12px;
    }
    .cell-compact-photos figure {
        margin: 0;
        text-align: center;
        flex: 1;
    }
    .cell-compact-photos figcaption {
        font-size: 11px;
        color: #6c757d;
        margin-bottom: 4px;
    }
    .cell-name { order: 2; }
    .cell-compact-actions { order: 2; justify-content: flex-start; }
    .cell-compact-meta { order: 1; }
    .cell-compact-attrs { order: 3; }
    .cell-compact-photos { order: 4; }
    .events-table .empty-row {
        display: block;
        text-align: center;
        border: none;
        box-shadow: none;
    }
    .pager { flex-direction: column; align-items: stretch; }
    .pager .btn, .pager form { width: 100%; }
    .pager form { justify-content: center; }
    .file-pick { width: 100%; }
    .card-body { padding: 14px; }
}

.live-status {
    font-weight: 600;
    padding: 2px 10px;
    border-radius: 999px;
    background: #e8f5e9;
    color: #1b5e20;
}
.live-status.live-off {
    background: #fce4ec;
    color: #880e4f;
}
@keyframes liveFlash {
    from { background-color: #fff8e1; }
    to { background-color: transparent; }
}
tr.event-row.live-new {
    animation: liveFlash 1.6s ease-out;
}
.events-table tbody {
    overflow-anchor: none;
}
