/* ==========================================================================
   AQUA — Smart Irrigation Control
   Design tokens
   ========================================================================== */
:root {
    /* Surfaces — deep "wet slate" rather than pure black */
    --bg: #0b1517;
    --bg-elevated: #0f1c1f;
    --surface: #132326;
    --surface-2: #182b2e;
    --surface-3: #1f373b;
    --border: #223a3d;
    --border-soft: #1a2e31;

    /* Text */
    --text: #eaf3f2;
    --text-dim: #8fa8a6;
    --text-faint: #5d7472;

    /* Brand — water flow */
    --primary: #2bc9c2;
    --primary-strong: #3fe0d8;
    --primary-dim: #1a8f8a;
    --on-primary: #06201f;

    /* Status */
    --success: #7bd66a;
    --success-dim: rgba(123, 214, 106, 0.14);
    --danger: #ef6a5f;
    --danger-dim: rgba(239, 106, 95, 0.14);
    --warning: #e3ab52;
    --warning-dim: rgba(227, 171, 82, 0.14);

    /* Shape & motion */
    --radius-sm: 8px;
    --radius: 14px;
    --radius-lg: 22px;
    --radius-pill: 999px;
    --ease: cubic-bezier(.4, 0, .2, 1);
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 16px 48px rgba(0, 0, 0, 0.5);

    --font: 'Cairo', 'Manrope', -apple-system, sans-serif;
    --font-mono: 'JetBrains Mono', ui-monospace, 'SF Mono', monospace;
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
    font-family: var(--font);
    background-color: var(--bg);
    background-image: radial-gradient(circle at 15% 0%, rgba(43, 201, 194, 0.07), transparent 45%);
    color: var(--text);
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
}
button, input, select { font-family: inherit; }
input:focus-visible, button:focus-visible, select:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}
::selection { background: var(--primary-dim); color: var(--text); }

.numeric { font-family: var(--font-mono); font-variant-numeric: tabular-nums; letter-spacing: 0.02em; }

.view { display: none; height: 100dvh; width: 100vw; flex-direction: column; }
.view.active { display: flex; }

/* ==========================================================================
   Connection screen
   ========================================================================== */
#view-connection {
    align-items: center;
    justify-content: center;
    padding: 24px;
    position: relative;
}

.lang-selector-container {
    position: absolute;
    top: 20px;
    inset-inline-end: 20px;
}
.lang-select {
    background: var(--surface-2);
    color: var(--text-dim);
    border: 1px solid var(--border);
    padding: 7px 14px;
    border-radius: var(--radius-pill);
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
    outline: none;
    transition: border-color .2s var(--ease), color .2s var(--ease);
    appearance: none;
}
.lang-select:hover { color: var(--text); border-color: var(--primary-dim); }

.connect-card {
    width: 100%;
    max-width: 380px;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.brand-mark {
    width: 84px;
    height: 84px;
    margin-bottom: 22px;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}
.brand-mark .ring {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 1px solid var(--primary-dim);
    opacity: 0;
    animation: ripple 3s var(--ease) infinite;
}
.brand-mark .ring:nth-child(2) { animation-delay: 1s; }
.brand-mark .ring:nth-child(3) { animation-delay: 2s; }
@keyframes ripple {
    0% { transform: scale(0.55); opacity: 0.55; }
    100% { transform: scale(1.35); opacity: 0; }
}
.brand-mark svg { position: relative; z-index: 1; width: 40px; height: 40px; color: var(--primary); filter: drop-shadow(0 0 14px rgba(43,201,194,0.45)); }

.connect-card h1 {
    font-size: 26px;
    font-weight: 800;
    letter-spacing: -0.01em;
    margin-bottom: 6px;
}
.connect-card .subtitle { color: var(--text-dim); margin-bottom: 34px; font-size: 15px; }

.btn {
    width: 100%;
    padding: 14px 18px;
    border: none;
    border-radius: var(--radius);
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    color: var(--text);
    transition: transform .12s var(--ease), filter .15s var(--ease), opacity .15s var(--ease);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}
.btn svg { width: 20px; height: 20px; flex-shrink: 0; }
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; pointer-events: none; }
.btn.primary { background: linear-gradient(180deg, var(--primary-strong), var(--primary)); color: var(--on-primary); box-shadow: 0 4px 16px rgba(43,201,194,0.25); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.secondary { background: var(--surface-3); color: var(--text); border: 1px solid var(--border); }
.btn.secondary:hover { background: var(--surface-2); border-color: var(--primary-dim); }
.btn.danger { background: var(--danger); color: #2a0d0a; }
.btn.ghost { background: transparent; color: var(--text-dim); border: 1px dashed var(--border); }
.btn.ghost:hover { color: var(--primary); border-color: var(--primary-dim); background: var(--surface-2); }
.btn.small { width: auto; padding: 8px 14px; font-size: 13px; border-radius: var(--radius-sm); }
.btn.block-loading { position: relative; color: transparent !important; }
.btn.block-loading::after {
    content: ""; position: absolute; width: 18px; height: 18px; border-radius: 50%;
    border: 2px solid rgba(255,255,255,0.35); border-top-color: #fff;
    animation: spin .7s linear infinite; color: initial;
}
@keyframes spin { to { transform: rotate(360deg); } }

.divider { margin: 22px 0; text-align: center; position: relative; width: 100%; }
.divider::before {
    content: ""; position: absolute; inset-inline-start: 0; top: 50%; width: 100%;
    height: 1px; background: var(--border); z-index: 1;
}
.divider span { background: var(--bg); padding: 0 12px; position: relative; z-index: 2; color: var(--text-faint); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; }

.mqtt-form { display: flex; flex-direction: column; gap: 12px; text-align: start; width: 100%; }
.mqtt-form label { font-size: 13px; color: var(--text-dim); font-weight: 700; }
.field-wrap { position: relative; display: flex; align-items: center; }
.field-wrap svg { position: absolute; inset-inline-start: 14px; width: 18px; height: 18px; color: var(--text-faint); pointer-events: none; }
.mqtt-form input {
    width: 100%; padding: 13px 14px 13px 42px; border-radius: var(--radius); border: 1px solid var(--border);
    background: var(--surface-2); color: var(--text); font-size: 15px; direction: ltr;
}
html[dir="rtl"] .mqtt-form input { padding: 13px 42px 13px 14px; }
html[dir="rtl"] .field-wrap svg { inset-inline-start: auto; inset-inline-end: 14px; }
.mqtt-form input:focus { border-color: var(--primary); background: var(--surface-3); }
.mqtt-form input::placeholder { color: var(--text-faint); }

/* ==========================================================================
   App shell — header
   ========================================================================== */
header {
    background: var(--bg-elevated);
    min-height: 62px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    border-bottom: 1px solid var(--border-soft);
    flex-shrink: 0;
    z-index: 10;
}
header h2 { font-size: 17px; font-weight: 800; letter-spacing: -0.01em; }
.header-title-group { display: flex; align-items: center; gap: 10px; min-width: 0; }
.header-title-group h2 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); box-shadow: 0 0 8px var(--success); flex-shrink: 0; }
.icon-group { display: flex; align-items: center; gap: 6px; }

.btn-icon {
    background: var(--surface); border: 1px solid var(--border-soft); width: 38px; height: 38px;
    border-radius: var(--radius-sm); cursor: pointer; color: var(--text-dim);
    display: flex; align-items: center; justify-content: center;
    transition: color .15s var(--ease), border-color .15s var(--ease), background .15s var(--ease), transform .12s var(--ease);
}
.btn-icon svg { width: 19px; height: 19px; }
.btn-icon:hover { color: var(--primary); border-color: var(--primary-dim); }
.btn-icon:active { transform: scale(0.94); }
.btn-icon.danger:hover { color: var(--danger); border-color: var(--danger); }

.content { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
html[dir="rtl"] .rtl-flip { transform: scaleX(-1); }

/* ==========================================================================
   Map & markers
   ========================================================================== */
#view-map { position: relative; }
#map-container { flex: 1; width: 100%; position: relative; z-index: 1; background: var(--bg); }

.valve-marker {
    background: var(--surface-3); border: 2px solid var(--text-dim);
    border-radius: 50%; width: 30px; height: 30px;
    box-shadow: var(--shadow-sm);
    display: flex; align-items: center; justify-content: center;
    color: var(--text); font-weight: 800; font-family: var(--font-mono); font-size: 10px;
    transition: background .2s var(--ease), border-color .2s var(--ease), box-shadow .2s var(--ease);
}
.valve-marker.active { background: var(--success); border-color: var(--success); color: #06210a; box-shadow: 0 0 0 6px var(--success-dim); }

.fab {
    position: absolute; z-index: 900;
    width: 50px; height: 50px; border-radius: 50%;
    background: var(--surface-2); border: 1px solid var(--border);
    box-shadow: var(--shadow-md); display: flex; align-items: center; justify-content: center;
    color: var(--primary); cursor: pointer; transition: transform .15s var(--ease), opacity .15s var(--ease);
}
.fab svg { width: 22px; height: 22px; }
.fab:active { transform: scale(0.92); }
#btn-gps-locate { bottom: 28px; inset-inline-end: 18px; }

/* Leaflet attribution restyle to fit dark theme */
.leaflet-control-attribution { background: rgba(11,21,23,0.75) !important; color: var(--text-faint) !important; }
.leaflet-control-attribution a { color: var(--text-dim) !important; }
.leaflet-control-zoom { display: none; }

/* ==========================================================================
   Bottom sheet — valve panel
   ========================================================================== */
.panel {
    position: absolute; bottom: -100%; inset-inline-start: 0; width: 100%;
    background: var(--bg-elevated); border-top-left-radius: var(--radius-lg); border-top-right-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg); z-index: 1000; border: 1px solid var(--border-soft); border-bottom: none;
    transition: bottom .32s var(--ease); display: flex; flex-direction: column;
    max-height: 82vh;
}
.panel.open { bottom: 0; }
.panel-handle { width: 36px; height: 4px; border-radius: var(--radius-pill); background: var(--border); margin: 10px auto 0; flex-shrink: 0; }
.panel-header { display: flex; justify-content: space-between; align-items: center; padding: 12px 18px 14px; border-bottom: 1px solid var(--border-soft); gap: 12px; }
.panel-header-title { display: flex; align-items: center; gap: 6px; min-width: 0; }
.panel-header-title h3 { font-size: 18px; font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.panel-header-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.panel-content { padding: 18px; overflow-y: auto; }

.pin-chip {
    display: inline-flex; align-items: center; gap: 6px; background: var(--surface); border: 1px solid var(--border);
    color: var(--text-dim); font-size: 12px; font-weight: 700; padding: 5px 10px; border-radius: var(--radius-pill);
    margin-bottom: 16px;
}
.pin-chip .numeric { color: var(--primary); }

/* Toggle card */
.toggle-group {
    display: flex; justify-content: space-between; align-items: center;
    background: var(--surface); border: 1px solid var(--border-soft); padding: 16px; border-radius: var(--radius);
}
.toggle-group label:first-child { font-weight: 700; font-size: 15px; }
.switch { position: relative; display: inline-block; width: 50px; height: 30px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; inset: 0; background: var(--surface-3); border: 1px solid var(--border); transition: .25s var(--ease); border-radius: var(--radius-pill); }
.slider:before { position: absolute; content: ""; height: 22px; width: 22px; inset-inline-start: 3px; top: 3px; background: var(--text-faint); transition: .25s var(--ease); border-radius: 50%; }
input:checked + .slider { background: var(--success); border-color: var(--success); }
input:checked + .slider:before { transform: translateX(20px); background: #06210a; }
html[dir="rtl"] input:checked + .slider:before { transform: translateX(-20px); }

.timer-box {
    background: var(--warning-dim); color: var(--warning); padding: 12px 14px; border-radius: var(--radius);
    display: flex; align-items: center; justify-content: center; gap: 8px; margin-top: 14px; border: 1px solid rgba(227,171,82,0.3);
    font-weight: 700; font-size: 14px;
}
.timer-box svg { width: 17px; height: 17px; }
.timer-box strong { font-family: var(--font-mono); font-size: 16px; letter-spacing: 0.02em; }

.section-label {
    font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.08em;
    color: var(--text-faint); margin: 22px 0 10px;
}
.section-label:first-of-type { margin-top: 22px; }

/* ==========================================================================
   Settings
   ========================================================================== */
.settings-form { padding: 18px 18px 100px; max-width: 560px; margin: 0 auto; }
.settings-card { background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--radius); padding: 6px 16px; margin-bottom: 6px; }
.form-group { padding: 14px 0; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--border-soft); }
.settings-card .form-group:last-child { border-bottom: none; }
.form-group.row { flex-direction: row; justify-content: space-between; align-items: center; gap: 12px; }
.form-group label { color: var(--text); font-size: 14px; font-weight: 700; }
.form-group .hint { color: var(--text-faint); font-size: 12px; font-weight: 400; margin-top: -4px; }
.form-group input[type="text"],
.form-group input[type="password"],
.form-group input[type="number"] {
    padding: 12px 14px; border-radius: var(--radius-sm); border: 1px solid var(--border);
    background: var(--surface-2); color: var(--text); font-size: 15px; outline: none;
    transition: border-color .15s var(--ease);
}
.form-group input:focus { border-color: var(--primary); }
.form-group input[type="checkbox"] {
    appearance: none; width: 46px; height: 27px; border-radius: var(--radius-pill); background: var(--surface-3);
    border: 1px solid var(--border); position: relative; cursor: pointer; flex-shrink: 0; transition: background .2s var(--ease);
}
.form-group input[type="checkbox"]::before {
    content: ""; position: absolute; width: 19px; height: 19px; border-radius: 50%; background: var(--text-faint);
    top: 3px; inset-inline-start: 3px; transition: .2s var(--ease);
}
.form-group input[type="checkbox"]:checked { background: var(--success); border-color: var(--success); }
.form-group input[type="checkbox"]:checked::before { transform: translateX(19px); background: #06210a; }
html[dir="rtl"] .form-group input[type="checkbox"]:checked::before { transform: translateX(-19px); }

.time-readout {
    background: var(--surface-2); border: 1px solid var(--border); padding: 12px; border-radius: var(--radius-sm);
    color: var(--primary); text-align: center; font-size: 19px; font-weight: 700; direction: ltr;
    font-family: var(--font-mono);
}

.settings-actions {
    position: sticky; bottom: 0; display: flex; flex-direction: column; gap: 10px; padding: 14px 0 4px;
    background: linear-gradient(180deg, transparent, var(--bg) 30%);
    margin-top: 4px;
}

/* ==========================================================================
   Modals
   ========================================================================== */
.modal {
    display: none; position: fixed; inset: 0; z-index: 2000;
    background: rgba(6, 12, 13, 0.72); backdrop-filter: blur(4px);
    align-items: flex-end; justify-content: center;
}
.modal.active { display: flex; animation: fade-in .18s var(--ease); }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.modal-content {
    background: var(--bg-elevated); padding: 22px; border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    width: 100%; max-width: 440px; border: 1px solid var(--border-soft); border-bottom: none;
    animation: sheet-up .22s var(--ease);
    max-height: 90vh; overflow-y: auto;
}
@keyframes sheet-up { from { transform: translateY(24px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@media (min-width: 480px) {
    .modal { align-items: center; }
    .modal-content { border-radius: var(--radius-lg); border-bottom: 1px solid var(--border-soft); animation: pop-in .18s var(--ease); }
    @keyframes pop-in { from { transform: scale(0.96); opacity: 0; } to { transform: scale(1); opacity: 1; } }
}
.modal-content h3 { font-size: 18px; font-weight: 800; margin-bottom: 18px; }
.modal-content .form-group { border-bottom: none; padding: 0 0 14px; }
.modal-content input[type="text"],
.modal-content input[type="number"],
.modal-content input[type="time"] {
    width: 100%; padding: 12px 14px; border-radius: var(--radius-sm); border: 1px solid var(--border);
    background: var(--surface-2); color: var(--text); font-size: 15px; outline: none;
}
.modal-content input:focus { border-color: var(--primary); }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }

/* Confirm dialog */
.confirm-icon { width: 46px; height: 46px; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin-bottom: 14px; }
.confirm-icon.danger { background: var(--danger-dim); color: var(--danger); }
.confirm-icon svg { width: 22px; height: 22px; }
#modal-confirm .modal-content { max-width: 360px; }
#modal-confirm p { color: var(--text-dim); font-size: 14px; line-height: 1.6; margin-bottom: 20px; }

/* Duration picker */
.duration-display { text-align: center; margin: 4px 0 22px; }
.duration-display .num { font-family: var(--font-mono); font-size: 52px; font-weight: 700; color: var(--primary); line-height: 1; }
.duration-display .unit { color: var(--text-dim); font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; margin-top: 4px; }
.duration-stepper { display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 22px; }
.stepper-btn {
    width: 46px; height: 46px; border-radius: 50%; border: 1px solid var(--border); background: var(--surface-2);
    color: var(--text); font-size: 20px; cursor: pointer; display: flex; align-items: center; justify-content: center;
    transition: border-color .15s var(--ease), color .15s var(--ease);
}
.stepper-btn:hover { border-color: var(--primary); color: var(--primary); }
.stepper-btn:active { transform: scale(0.94); }
.duration-presets { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin-bottom: 4px; }
.preset-chip {
    padding: 8px 14px; border-radius: var(--radius-pill); border: 1px solid var(--border); background: var(--surface-2);
    color: var(--text-dim); font-size: 13px; font-weight: 700; cursor: pointer; transition: all .15s var(--ease);
}
.preset-chip:hover, .preset-chip.selected { border-color: var(--primary); color: var(--primary); background: rgba(43,201,194,0.08); }

/* Day pills */
.day-pills { display: flex; justify-content: space-between; margin-top: 4px; gap: 4px; }
.day-pill input { display: none; }
.day-pill span {
    display: flex; align-items: center; justify-content: center; width: 36px; height: 36px;
    background: var(--surface-2); border: 1px solid var(--border); border-radius: 50%; cursor: pointer;
    color: var(--text-dim); font-size: 13px; font-weight: 700; transition: all .15s var(--ease);
}
.day-pill input:checked + span { background: var(--primary); border-color: var(--primary); color: var(--on-primary); box-shadow: 0 0 12px rgba(43,201,194,0.4); }

/* Schedule cards */
.sched-card {
    background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--radius); padding: 14px;
    margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center;
    border-inline-start: 3px solid var(--border); gap: 10px; transition: border-color .15s var(--ease);
}
.sched-card.active-sched { border-inline-start-color: var(--success); }
.sched-info { min-width: 0; }
.sched-info h4 { margin: 0; color: var(--text); font-size: 19px; font-weight: 800; direction: ltr; text-align: start; font-family: var(--font-mono); }
.sched-info p { margin: 5px 0 0; font-size: 12px; color: var(--text-dim); }
.sched-status { display: inline-flex; align-items: center; gap: 5px; font-weight: 700; }
.sched-status::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.sched-actions { display: flex; gap: 6px; flex-shrink: 0; }
.btn-edit, .btn-delete {
    border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2);
    color: var(--text-dim); width: 34px; height: 34px; display: flex; align-items: center; justify-content: center;
    cursor: pointer; transition: all .15s var(--ease);
}
.btn-edit svg, .btn-delete svg { width: 15px; height: 15px; }
.btn-edit:hover { color: var(--primary); border-color: var(--primary-dim); }
.btn-delete:hover { color: var(--danger); border-color: var(--danger); }

.empty-state { text-align: center; padding: 28px 10px; color: var(--text-faint); font-size: 13px; }
.empty-state svg { width: 30px; height: 30px; margin-bottom: 10px; opacity: 0.6; }

/* ==========================================================================
   Toasts
   ========================================================================== */
#toast-container {
    position: fixed; bottom: 22px; left: 0; right: 0; z-index: 3000;
    display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; padding: 0 16px;
}
.toast {
    background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
    padding: 12px 16px; border-radius: var(--radius); box-shadow: var(--shadow-md);
    font-size: 13.5px; font-weight: 700; max-width: 420px; width: 100%;
    display: flex; align-items: center; gap: 10px; border-inline-start: 3px solid var(--primary);
    animation: toast-in .25s var(--ease);
}
.toast svg { width: 18px; height: 18px; flex-shrink: 0; }
.toast.success { border-inline-start-color: var(--success); }
.toast.success svg { color: var(--success); }
.toast.error { border-inline-start-color: var(--danger); }
.toast.error svg { color: var(--danger); }
.toast.info svg { color: var(--primary); }
.toast.hide { animation: toast-out .2s var(--ease) forwards; }
@keyframes toast-in { from { transform: translateY(12px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes toast-out { to { transform: translateY(8px); opacity: 0; } }

/* Scrollbars (webkit) */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-track { background: transparent; }
