/* ═══════════════════════════════════════════════════════════
   PatrolWizzard V6 — Components
   ═══════════════════════════════════════════════════════════ */

/* ── Buttons ───────────────────────────────────────────── */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
    padding: var(--sp-2) var(--sp-4); font-size: var(--text-sm);
    font-weight: 500; border-radius: var(--radius-md);
    border: 1px solid transparent; cursor: pointer;
    transition: all var(--transition-fast);
    white-space: nowrap; line-height: 1.4;
    text-decoration: none;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-sm { padding: var(--sp-1) var(--sp-3); font-size: var(--text-xs); }
.btn-lg { padding: var(--sp-3) var(--sp-6); font-size: var(--text-base); }
.btn-block { width: 100%; }

.btn-primary {
    background: var(--pw-primary); color: #fff; border-color: var(--pw-primary);
}
.btn-primary:hover:not(:disabled) { background: var(--pw-primary-dark); }

.btn-secondary {
    background: var(--bg-card); color: var(--text-secondary);
    border-color: var(--border-default);
}
.btn-secondary:hover:not(:disabled) { background: var(--bg-body); border-color: var(--border-focus); }

.btn-success { background: var(--color-success); color: #fff; }
.btn-success:hover:not(:disabled) { filter: brightness(0.9); }
.btn-danger { background: var(--color-danger); color: #fff; }
.btn-danger:hover:not(:disabled) { filter: brightness(0.9); }
.btn-warning { background: var(--color-warning); color: #fff; }
.btn-warning:hover:not(:disabled) { filter: brightness(0.9); }

.btn-ghost {
    background: transparent; color: var(--text-secondary); border-color: transparent;
}
.btn-ghost:hover:not(:disabled) { background: var(--bg-body); color: var(--text-primary); }

.btn-icon {
    width: 34px; height: 34px; padding: 0;
    border-radius: var(--radius-md);
}

.btn .spinner {
    width: 14px; height: 14px; border: 2px solid rgba(255,255,255,0.3);
    border-top-color: #fff; border-radius: 50%;
    animation: pw-spin 0.6s linear infinite;
}

/* ── Cards ─────────────────────────────────────────────── */
.card {
    background: var(--bg-card); border: 1px solid var(--border-light);
    border-radius: var(--radius-lg); box-shadow: var(--shadow-sm);
}
.card-header {
    padding: var(--sp-4) var(--sp-5);
    border-bottom: 1px solid var(--border-light);
    display: flex; align-items: center; justify-content: space-between;
}
.card-header h3 {
    font-size: var(--text-md); font-weight: 600; color: var(--text-primary);
}
.card-body { padding: var(--sp-5); }
.card-footer {
    padding: var(--sp-3) var(--sp-5);
    border-top: 1px solid var(--border-light);
    display: flex; align-items: center; justify-content: space-between;
}

/* ── Stat Cards ────────────────────────────────────────── */
.stats-grid {
    display: grid; gap: var(--sp-4);
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    margin-bottom: var(--sp-6);
}
.stat-card {
    background: var(--bg-card); border: 1px solid var(--border-light);
    border-radius: var(--radius-lg); padding: var(--sp-5);
    box-shadow: var(--shadow-sm);
}
.stat-card .stat-label {
    font-size: var(--text-xs); color: var(--text-muted);
    text-transform: uppercase; letter-spacing: 0.5px; font-weight: 500;
    margin-bottom: var(--sp-1);
}
.stat-card .stat-value {
    font-size: var(--text-2xl); font-weight: 700; color: var(--text-primary);
    letter-spacing: -0.5px;
}
.stat-card .stat-change {
    font-size: var(--text-xs); margin-top: var(--sp-1);
    display: flex; align-items: center; gap: var(--sp-1);
}
.stat-card .stat-change.up { color: var(--color-success); }
.stat-card .stat-change.down { color: var(--color-danger); }
.stat-card .stat-icon {
    width: 40px; height: 40px; border-radius: var(--radius-lg);
    display: flex; align-items: center; justify-content: center;
    margin-bottom: var(--sp-3);
}

/* ── Forms ─────────────────────────────────────────────── */
.form-group { margin-bottom: var(--sp-4); }
.form-label {
    display: block; font-size: var(--text-sm); font-weight: 500;
    color: var(--text-primary); margin-bottom: var(--sp-1);
}
.form-label .required { color: var(--color-danger); margin-left: 2px; }
.form-hint {
    font-size: var(--text-xs); color: var(--text-muted); margin-top: var(--sp-1);
}
.form-error {
    font-size: var(--text-xs); color: var(--color-danger); margin-top: var(--sp-1);
}
.form-input, .form-select, .form-textarea {
    width: 100%; padding: var(--sp-2) var(--sp-3);
    background: var(--bg-input); border: 1px solid var(--border-default);
    border-radius: var(--radius-md); font-size: var(--text-sm);
    color: var(--text-primary); transition: border-color var(--transition-fast);
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
    outline: none; border-color: var(--border-focus);
    box-shadow: 0 0 0 3px var(--pw-primary-50);
}
.form-input.error, .form-select.error, .form-textarea.error {
    border-color: var(--border-error);
    box-shadow: 0 0 0 3px var(--color-danger-bg);
}
.form-textarea { resize: vertical; min-height: 80px; }
.form-select { cursor: pointer; }

.form-row {
    display: grid; gap: var(--sp-4);
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
.form-check {
    display: flex; align-items: center; gap: var(--sp-2); cursor: pointer;
}
.form-check input[type="checkbox"], .form-check input[type="radio"] {
    width: 16px; height: 16px; cursor: pointer; accent-color: var(--pw-primary);
}
.form-actions {
    display: flex; gap: var(--sp-2); justify-content: flex-end;
    padding-top: var(--sp-4); border-top: 1px solid var(--border-light);
    margin-top: var(--sp-4);
}

.input-group {
    display: flex; align-items: stretch;
}
.input-group .form-input { border-radius: var(--radius-md) 0 0 var(--radius-md); }
.input-group .btn { border-radius: 0 var(--radius-md) var(--radius-md) 0; }
.input-group .input-prefix, .input-group .input-suffix {
    padding: var(--sp-2) var(--sp-3); background: var(--bg-body);
    border: 1px solid var(--border-default); color: var(--text-muted);
    font-size: var(--text-sm); display: flex; align-items: center;
}

/* ── Tables ────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
.data-table {
    width: 100%; border-collapse: collapse; font-size: var(--text-sm);
}
.data-table thead th {
    padding: var(--sp-3) var(--sp-4); text-align: left;
    font-size: var(--text-xs); font-weight: 600;
    text-transform: uppercase; letter-spacing: 0.5px;
    color: var(--text-muted); background: var(--bg-body);
    border-bottom: 1px solid var(--border-light);
    white-space: nowrap; cursor: default;
    user-select: none;
}
.data-table thead th.sortable { cursor: pointer; }
.data-table thead th.sortable:hover { color: var(--text-primary); }
.data-table thead th .sort-icon { margin-left: var(--sp-1); opacity: 0.3; }
.data-table thead th.sort-asc .sort-icon,
.data-table thead th.sort-desc .sort-icon { opacity: 1; color: var(--pw-primary); }

.data-table tbody td {
    padding: var(--sp-3) var(--sp-4);
    border-bottom: 1px solid var(--border-light);
    color: var(--text-secondary); vertical-align: middle;
}
.data-table tbody tr:hover td { background: var(--pw-primary-50); }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table .cell-primary { color: var(--text-primary); font-weight: 500; }
.data-table .cell-mono { font-family: var(--font-mono); font-size: var(--text-xs); }
.data-table .cell-actions {
    display: flex; gap: var(--sp-1); justify-content: flex-end;
}

.table-toolbar {
    display: flex; align-items: center; justify-content: space-between;
    gap: var(--sp-3); flex-wrap: wrap; margin-bottom: var(--sp-4);
}
.table-toolbar .search-box {
    position: relative; flex: 1; max-width: 300px;
}
.table-toolbar .search-box input {
    width: 100%; padding: var(--sp-2) var(--sp-3) var(--sp-2) 34px;
    background: var(--bg-card); border: 1px solid var(--border-default);
    border-radius: var(--radius-md); font-size: var(--text-sm);
}
.table-toolbar .search-box .search-icon {
    position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
    color: var(--text-muted);
}
.table-toolbar .filters { display: flex; gap: var(--sp-2); }

.table-pagination {
    display: flex; align-items: center; justify-content: space-between;
    padding: var(--sp-3) 0; font-size: var(--text-sm); color: var(--text-muted);
}
.table-pagination .page-buttons { display: flex; gap: var(--sp-1); }
.table-pagination .page-btn {
    width: 32px; height: 32px; border: 1px solid var(--border-light);
    background: var(--bg-card); border-radius: var(--radius-md);
    cursor: pointer; display: flex; align-items: center; justify-content: center;
    font-size: var(--text-sm); color: var(--text-secondary);
    transition: all var(--transition-fast);
}
.table-pagination .page-btn:hover { border-color: var(--pw-primary); color: var(--pw-primary); }
.table-pagination .page-btn.active {
    background: var(--pw-primary); border-color: var(--pw-primary); color: #fff;
}
.table-pagination .page-btn:disabled { opacity: 0.3; cursor: not-allowed; }

/* ── Badges ────────────────────────────────────────────── */
.badge {
    display: inline-flex; align-items: center; gap: var(--sp-1);
    padding: 2px 8px; border-radius: var(--radius-full);
    font-size: var(--text-xs); font-weight: 500;
    white-space: nowrap;
}
.badge-primary { background: var(--pw-primary-100); color: var(--pw-primary); }
.badge-success { background: var(--color-success-bg); color: var(--color-success); }
.badge-warning { background: var(--color-warning-bg); color: var(--color-warning); }
.badge-danger { background: var(--color-danger-bg); color: var(--color-danger); }
.badge-info { background: var(--color-info-bg); color: var(--color-info); }
.badge-neutral {
    background: rgba(100,116,139,0.1); color: var(--text-secondary);
}
.badge-dot::before {
    content: ''; width: 6px; height: 6px; border-radius: 50%;
    background: currentColor;
}

/* ── Tabs ──────────────────────────────────────────────── */
.tabs {
    display: flex; gap: 0; border-bottom: 1px solid var(--border-light);
    margin-bottom: var(--sp-5);
}
.tab-btn {
    padding: var(--sp-3) var(--sp-4); font-size: var(--text-sm);
    color: var(--text-muted); background: none; border: none;
    border-bottom: 2px solid transparent; cursor: pointer;
    font-weight: 500; transition: all var(--transition-fast);
}
.tab-btn:hover { color: var(--text-primary); }
.tab-btn.active {
    color: var(--pw-primary); border-bottom-color: var(--pw-primary);
}
.tab-content { display: none; }
.tab-content.active { display: block; animation: pw-fade-in 200ms ease; }

/* ── Alerts ────────────────────────────────────────────── */
.alert {
    padding: var(--sp-3) var(--sp-4); border-radius: var(--radius-md);
    font-size: var(--text-sm); display: flex; align-items: flex-start; gap: var(--sp-3);
    margin-bottom: var(--sp-4);
}
.alert-info { background: var(--color-info-bg); color: var(--color-info); border: 1px solid rgba(59,130,246,0.2); }
.alert-success { background: var(--color-success-bg); color: var(--color-success); border: 1px solid rgba(16,185,129,0.2); }
.alert-warning { background: var(--color-warning-bg); color: var(--color-warning); border: 1px solid rgba(245,158,11,0.2); }
.alert-danger { background: var(--color-danger-bg); color: var(--color-danger); border: 1px solid rgba(239,68,68,0.2); }

/* ── Empty State ───────────────────────────────────────── */
.empty-state {
    text-align: center; padding: var(--sp-12) var(--sp-6);
    color: var(--text-muted);
}
.empty-state .empty-icon { font-size: 48px; margin-bottom: var(--sp-4); opacity: 0.3; }
.empty-state h3 { font-size: var(--text-md); color: var(--text-secondary); margin-bottom: var(--sp-2); }
.empty-state p { font-size: var(--text-sm); margin-bottom: var(--sp-4); }

/* ── Loading ───────────────────────────────────────────── */
.loading-spinner {
    display: flex; align-items: center; justify-content: center;
    padding: var(--sp-10);
}
.loading-spinner::after {
    content: ''; width: 32px; height: 32px;
    border: 3px solid var(--border-light);
    border-top-color: var(--pw-primary); border-radius: 50%;
    animation: pw-spin 0.7s linear infinite;
}
.skeleton {
    background: linear-gradient(90deg, var(--border-light) 25%, #e8ecf1 50%, var(--border-light) 75%);
    background-size: 200% 100%;
    animation: pw-pulse 1.5s ease-in-out infinite;
    border-radius: var(--radius-md);
}

/* ── Dropdown ──────────────────────────────────────────── */
.dropdown { position: relative; }
.dropdown-menu {
    position: absolute; top: calc(100% + 4px); right: 0;
    background: var(--bg-card); border: 1px solid var(--border-light);
    border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
    min-width: 180px; z-index: 50; display: none;
    animation: pw-scale-in 150ms ease;
}
.dropdown.open .dropdown-menu { display: block; }
.dropdown-item {
    display: flex; align-items: center; gap: var(--sp-2);
    padding: var(--sp-2) var(--sp-4); font-size: var(--text-sm);
    color: var(--text-secondary); cursor: pointer; transition: all var(--transition-fast);
}
.dropdown-item:hover { background: var(--pw-primary-50); color: var(--pw-primary); }
.dropdown-item:first-child { border-radius: var(--radius-lg) var(--radius-lg) 0 0; }
.dropdown-item:last-child { border-radius: 0 0 var(--radius-lg) var(--radius-lg); }
.dropdown-divider { height: 1px; background: var(--border-light); margin: var(--sp-1) 0; }

/* ── Tooltip ───────────────────────────────────────────── */
[data-tooltip] { position: relative; }
[data-tooltip]::after {
    content: attr(data-tooltip);
    position: absolute; bottom: calc(100% + 6px); left: 50%;
    transform: translateX(-50%); padding: 4px 8px;
    background: var(--bg-tooltip); color: #fff;
    font-size: var(--text-xs); border-radius: var(--radius-sm);
    white-space: nowrap; opacity: 0; pointer-events: none;
    transition: opacity var(--transition-fast);
}
[data-tooltip]:hover::after { opacity: 1; }

/* ── Avatar ────────────────────────────────────────────── */
.avatar {
    width: 32px; height: 32px; border-radius: var(--radius-full);
    display: flex; align-items: center; justify-content: center;
    font-size: var(--text-xs); font-weight: 600; flex-shrink: 0;
}
.avatar-sm { width: 24px; height: 24px; font-size: 10px; }
.avatar-lg { width: 40px; height: 40px; font-size: var(--text-sm); }
.avatar-xl { width: 56px; height: 56px; font-size: var(--text-lg); }

/* ── Utility ───────────────────────────────────────────── */
.text-success { color: var(--color-success); }
.text-danger { color: var(--color-danger); }
.text-warning { color: var(--color-warning); }
.text-muted { color: var(--text-muted); }
.text-right { text-align: right; }
.text-center { text-align: center; }
.font-mono { font-family: var(--font-mono); }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.flex { display: flex; }
.flex-center { display: flex; align-items: center; }
.flex-between { display: flex; align-items: center; justify-content: space-between; }
.gap-2 { gap: var(--sp-2); }
.gap-4 { gap: var(--sp-4); }
.mt-2 { margin-top: var(--sp-2); }
.mt-4 { margin-top: var(--sp-4); }
.mb-2 { margin-bottom: var(--sp-2); }
.mb-4 { margin-bottom: var(--sp-4); }
.hidden { display: none !important; }


/* ── Rich Text Editor ─────────────────────────────────── */
.richtext-wrap {
    border: 1px solid var(--border-default, var(--border-light));
    border-radius: var(--radius-md);
    overflow: hidden;
}
.richtext-toolbar {
    display: flex;
    gap: 2px;
    padding: 6px 8px;
    background: var(--bg-subtle, #f8fafc);
    border-bottom: 1px solid var(--border-light);
    flex-wrap: wrap;
}
.richtext-toolbar button {
    border: none;
    background: transparent;
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1;
}
.richtext-toolbar button:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}
.richtext-sep {
    width: 1px;
    background: var(--border-light);
    margin: 0 4px;
}
.richtext-editor {
    min-height: 180px;
    max-height: 400px;
    overflow-y: auto;
    padding: 12px;
    font-size: var(--text-sm);
    line-height: 1.6;
    outline: none;
}
.richtext-editor:focus {
    box-shadow: inset 0 0 0 2px var(--pw-primary-50);
}
.richtext-editor h3 { font-size: 15px; font-weight: 600; margin: 8px 0 4px; }
.richtext-editor ul, .richtext-editor ol { padding-left: 20px; margin: 4px 0; }
.richtext-editor li { margin: 2px 0; }

/* ── Geofence Split Layout ────────────────────────────── */
.geo-toolbar {
    display: flex; gap: var(--sp-2); padding: var(--sp-3);
    border-bottom: 1px solid var(--border-light);
    align-items: center; flex-wrap: wrap;
}
.geo-count { font-size: var(--text-sm); color: var(--text-muted); }
.geo-size-toggle {
    display: flex; gap: 2px; background: var(--bg-subtle, #f1f5f9);
    border-radius: var(--radius-md); padding: 2px;
}
.geo-size-btn {
    background: none; border: none; cursor: pointer;
    padding: 4px 8px; border-radius: var(--radius-sm);
    color: var(--text-muted); display: flex; align-items: center;
    transition: all .15s;
}
.geo-size-btn.active {
    background: var(--bg-primary, #fff); color: var(--text-primary);
    box-shadow: 0 1px 2px rgba(0,0,0,.08);
}
.geo-size-btn:hover:not(.active) { color: var(--text-secondary); }

.geo-split {
    display: flex; height: 500px;
    border-radius: 0 0 var(--radius-lg) var(--radius-lg);
    overflow: hidden; transition: all .25s ease;
}
.geo-list-panel {
    overflow-y: auto; border-right: 1px solid var(--border-light);
    transition: width .25s ease, min-width .25s ease;
}
.geo-map-panel {
    flex: 1; position: relative;
    transition: all .25s ease;
}
.geo-map-panel #geo-map {
    position: absolute; top: 0; left: 0; right: 0; bottom: 0;
}

/* Size: 1/3 map (default) */
.geo-split[data-size="third"] .geo-list-panel { width: 66%; min-width: 66%; }
.geo-split[data-size="third"] .geo-map-panel { width: 34%; }

/* Size: 1/2 map */
.geo-split[data-size="half"] .geo-list-panel { width: 50%; min-width: 50%; }
.geo-split[data-size="half"] .geo-map-panel { width: 50%; }

/* Size: full map */
.geo-split[data-size="full"] .geo-list-panel { width: 0; min-width: 0; overflow: hidden; border: none; }
.geo-split[data-size="full"] .geo-map-panel { width: 100%; }

/* Geofence cards */
.geo-card {
    padding: var(--sp-3); border-bottom: 1px solid var(--border-light);
    cursor: pointer; transition: background .15s;
}
.geo-card:hover { background: var(--bg-hover, #f8fafc); }
.geo-card:last-child { border-bottom: none; }
.geo-card-header {
    display: flex; align-items: center; gap: var(--sp-2);
    margin-bottom: 4px;
}
.geo-card-dot {
    width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
}
.geo-card-name {
    font-size: var(--text-sm); flex: 1; overflow: hidden;
    text-overflow: ellipsis; white-space: nowrap;
}
.geo-card-meta {
    font-size: var(--text-xs); color: var(--text-muted);
    padding-left: 16px; margin-bottom: 4px;
}
.geo-card-actions {
    display: flex; gap: 2px; padding-left: 12px;
}
.geo-card-actions .btn { opacity: 0; transition: opacity .15s; }
.geo-card:hover .geo-card-actions .btn { opacity: 1; }

/* Mobile: stack vertically */
@media (max-width: 768px) {
    .geo-split { flex-direction: column; }
    .geo-split .geo-list-panel { width: 100% !important; min-width: 100% !important; max-height: 200px; border-right: none; border-bottom: 1px solid var(--border-light); }
    .geo-split .geo-map-panel { width: 100% !important; min-height: 300px; }
    .geo-split[data-size="full"] .geo-list-panel { max-height: 0; min-width: 100% !important; }
    .geo-size-toggle { display: none; }
}

/* ── Overview Split Layout ────────────────────────────── */
.ov-split {
    display: flex; height: 400px;
}
.ov-info {
    flex: 1; min-width: 0; overflow-y: auto;
}
.ov-map-wrap {
    width: 360px; min-width: 280px; padding: var(--sp-3);
    position: relative; flex-shrink: 0;
    border-left: 1px solid var(--border-light);
}
.ov-map-wrap #ov-leaflet-map {
    position: absolute; top: var(--sp-3); left: var(--sp-3); right: var(--sp-3); bottom: var(--sp-3);
    border-radius: var(--radius-md); z-index: 1;
}
.ov-map-empty {
    position: absolute; top: 0; left: 0; right: 0; bottom: 0;
    display: flex; align-items: center; justify-content: center;
    font-size: var(--text-sm); color: var(--text-muted);
    background: var(--bg-subtle, #f8fafc); border-radius: var(--radius-md);
    margin: var(--sp-3);
}
@media (max-width: 768px) {
    .ov-split { flex-direction: column-reverse; }
    .ov-map-wrap { width: 100%; height: 220px; border-left: none; border-bottom: 1px solid var(--border-light); }
    .ov-split { height: auto; }
}

/* ── Toggle Switch ─────────────────────────────────────── */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
    flex-shrink: 0;
    cursor: pointer;
}
.toggle-switch input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
    margin: 0;
}
.toggle-track {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: var(--border-default);
    border-radius: 24px;
    transition: background 0.2s ease;
    cursor: pointer;
}
.toggle-thumb {
    position: absolute;
    height: 18px;
    width: 18px;
    left: 3px;
    bottom: 3px;
    background: #fff;
    border-radius: 50%;
    transition: left 0.2s ease;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
    pointer-events: none;
}
.toggle-switch input:checked ~ .toggle-track  { background: var(--pw-primary); }
.toggle-switch input:checked ~ .toggle-thumb  { left: 23px; }
