/* ============================================
   LD ICT Chicken Door - dark responsive theme
   ============================================ */

:root {
    /* Surfaces */
    --bg: #12141a;
    --surface: #1c1f27;
    --surface-2: #262a35;
    --surface-3: #30354180;
    --border: #333846;

    /* Text */
    --text: #e8eaed;
    --text-muted: #9aa0ac;

    /* Accents */
    --accent: #4a9eff;
    --accent-hover: #3d8ae6;
    --accent-text: #ffffff;
    --success: #3fb950;
    --warning: #d29922;
    --danger: #f85149;

    --radius: 10px;
    --radius-sm: 6px;
}

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

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

body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg);
    color: var(--text);
    display: flex;
    justify-content: center;
    align-items: flex-start;
    /* dvh avoids the mobile browser-chrome jump that 100vh causes */
    min-height: 100dvh;
    margin: 0;
    /* In iOS standalone mode the page extends under the status bar and
       home indicator (viewport-fit=cover + black-translucent), so the
       safe-area insets must never be smaller than the base padding. */
    padding:
        calc(max(clamp(0.75rem, 3vw, 2.5rem), env(safe-area-inset-top)) + 1rem)
        max(clamp(0.75rem, 3vw, 1.5rem), env(safe-area-inset-right))
        max(clamp(0.75rem, 3vw, 2.5rem), env(safe-area-inset-bottom))
        max(clamp(0.75rem, 3vw, 1.5rem), env(safe-area-inset-left));
    line-height: 1.5;
}

.container {
    background: var(--surface);
    padding: clamp(1.25rem, 4vw, 2.25rem);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    text-align: center;
    width: 100%;
    max-width: 640px;
}

h1 {
    margin: 0 0 1.25rem;
    font-size: clamp(1.35rem, 5vw, 2rem);
    font-weight: 700;
    letter-spacing: -0.02em;
    line-height: 1.2;
    /* Long custom names wrap instead of overflowing */
    overflow-wrap: anywhere;
}

/* -------------------------
   Status card
   ------------------------- */

.status-card {
    margin: 20px 0;
    padding: clamp(0.85rem, 3vw, 1.25rem);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

.status-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    margin: 0.4rem 0;
    text-align: left;
}

.status-row .label {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.status-row .value {
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

#door-status.is-open {
    color: var(--success);
}

#door-status.is-closed {
    color: var(--text-muted);
}

#door-status.is-moving {
    color: var(--warning);
}

.progress-container {
    background-color: var(--surface-3);
    border: 1px solid var(--border);
    border-radius: 999px;
    height: 18px;
    width: 100%;
    margin: 12px 0 8px;
    overflow: hidden;
}

.progress-bar {
    background-color: var(--success);
    height: 100%;
    width: 0%;
    border-radius: 999px;
    transition: width 0.1s linear;
}

/* -------------------------
   Controls
   ------------------------- */

.controls {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
    margin-bottom: 16px;
}

button {
    flex: 1 1 140px;
    min-height: 44px; /* comfortable tap target */
    padding: 10px 18px;
    font-size: 1rem;
    font-family: inherit;
    font-weight: 600;
    cursor: pointer;
    background-color: var(--accent);
    color: var(--accent-text);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    transition: background-color 0.15s ease, opacity 0.15s ease;
}

button:hover:not(:disabled) {
    background-color: var(--accent-hover);
}

button:active:not(:disabled) {
    opacity: 0.85;
}

button:disabled {
    background-color: var(--surface-2);
    color: var(--text-muted);
    border-color: var(--border);
    cursor: not-allowed;
}

button.stop {
    --accent: #f85149;
    --accent-hover: #d93f37;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* -------------------------
   Forms
   ------------------------- */

.field {
    text-align: left;
    margin-bottom: 1rem;
}

label {
    display: block;
    text-align: left;
    font-weight: 600;
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.35rem;
    text-transform: capitalize;
}

input {
    display: block;
    width: 100%;
    min-height: 44px;
    padding: 8px 12px;
    font-size: 1rem;
    font-family: inherit;
    color: var(--text);
    background-color: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

input:focus,
select:focus {
    border-color: var(--accent);
    outline: none;
}

select {
    display: block;
    width: 100%;
    min-height: 44px;
    padding: 8px 12px;
    font-size: 1rem;
    font-family: inherit;
    color: var(--text);
    background-color: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
}

/* -------------------------
   Location section & side-by-side fields
   ------------------------- */

.section {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background-color: var(--surface-2);
    padding: 0.85rem 1rem 1rem;
    margin: 0 0 1rem;
    text-align: left;
}

.section > legend {
    padding: 0 0.4rem;
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-muted);
}

/* Latitude and longitude share a row on wider screens */
.field-row {
    display: flex;
    gap: 0.75rem;
}

.field-row > .field {
    flex: 1 1 0;
    min-width: 0;
}

.section .controls {
    margin: 0.25rem 0 0;
}

/* -------------------------
   Modal
   ------------------------- */

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
    display: flex;
    align-items: center;
    justify-content: center;
    padding:
        max(1rem, env(safe-area-inset-top))
        max(1rem, env(safe-area-inset-right))
        max(1rem, env(safe-area-inset-bottom))
        max(1rem, env(safe-area-inset-left));
    z-index: 50;
}

.modal-backdrop[hidden] {
    display: none;
}

.modal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
    width: 100%;
    max-width: 460px;
    max-height: 85dvh;
    display: flex;
    flex-direction: column;
    text-align: left;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.9rem 1rem;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 700;
}

.modal-close {
    flex: none;
    width: 44px;
    min-height: 44px;
    padding: 0;
    font-size: 1.5rem;
    line-height: 1;
    background: transparent;
    color: var(--text-muted);
    border: 1px solid transparent;
}

.modal-close:hover:not(:disabled) {
    background: var(--surface-2);
    color: var(--text);
}

.modal-body {
    padding: 1rem;
    overflow-y: auto;
}

.modal-footer {
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
}

.modal-footer button {
    flex: 0 0 auto;
    min-width: 110px;
}

/* -------------------------
   Address lookup results
   ------------------------- */

.results-heading {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
    margin: 0 0 0.35rem;
    text-align: left;
}

.results {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 1rem;
}

.result-item {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
    padding: 10px 12px;
    background-color: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-weight: 500;
    text-align: left;
    cursor: pointer;
}

.result-item:hover:not(:disabled) {
    background-color: var(--surface-3);
    border-color: var(--accent);
}

.result-item small {
    color: var(--text-muted);
    font-weight: 400;
    font-size: 0.8rem;
    font-variant-numeric: tabular-nums;
}

.highlight {
    border-color: var(--success) !important;
    box-shadow: 0 0 0 2px rgba(63, 185, 80, 0.3);
}

/* Boolean settings: checkbox beside its label rather than above it */
.field-check {
    display: flex;
    align-items: center;
    min-height: 44px;
    padding: 4px 0;
}

.field-check .checkbox-label {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin: 0;
    font-weight: 500;
    font-size: 0.95rem;
    color: var(--text);
    text-transform: none;
    cursor: pointer;
    width: 100%;
}

.field-check input[type="checkbox"] {
    width: 20px;
    height: 20px;
    min-height: 0;
    flex: none;
    accent-color: var(--accent);
    cursor: pointer;
    margin: 0;
}

input::placeholder {
    color: var(--text-muted);
}

/* -------------------------
   Navigation & notes
   ------------------------- */

.nav {
    margin-top: 20px;
    font-size: 0.9rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
    justify-content: center;
}

.nav a {
    color: var(--accent);
    text-decoration: none;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
}

.nav a:hover {
    text-decoration: underline;
}

/* Logout: a POST form rendered to look like the other nav links */
.nav-form {
    display: inline-flex;
}

.nav-form .link-button {
    background: none;
    border: none;
    padding: 0;
    margin: 0;
    flex: none;
    width: auto;
    color: var(--accent);
    font: inherit;
    font-size: 0.9rem;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    cursor: pointer;
    box-shadow: none;
}

.nav-form .link-button:hover {
    text-decoration: underline;
    background: none;
    color: var(--accent);
}

/* Login page: same shell, narrower panel */
.login-container {
    max-width: 22rem;
}

.login-container .status-card {
    margin-bottom: 0.75rem;
}

.login-container .status-card p {
    margin: 0;
    color: var(--text-muted);
    font-size: 0.9rem;
    line-height: 1.45;
}

.note {
    font-size: 0.85rem;
    color: var(--warning);
    margin: 0.5rem 0;
    overflow-wrap: anywhere;
}

.note.offline {
    color: var(--danger);
    font-weight: 600;
}

.note.error {
    color: var(--danger);
}

/* Heading on the service worker's offline fallback page */
.offline-heading {
    margin: 0 0 0.5rem;
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-muted);
}

/* -------------------------
   Advanced (collapsible)
   ------------------------- */

.advanced {
    text-align: left;
    margin: 4px 0 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background-color: var(--surface-2);
}

.advanced > summary {
    cursor: pointer;
    padding: 12px 14px;
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--text-muted);
    list-style: none;
    border-radius: var(--radius-sm);
}

.advanced > summary::-webkit-details-marker {
    display: none;
}

/* Chevron rotates to hint at the collapsed state */
.advanced > summary::before {
    content: "";
    width: 0.4rem;
    height: 0.4rem;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: rotate(-45deg);
    transition: transform 0.15s ease;
    flex: none;
}

.advanced[open] > summary::before {
    transform: rotate(45deg);
}

.advanced > summary:hover {
    color: var(--text);
}

.advanced > summary:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.advanced .controls {
    margin: 0;
    padding: 0 14px 4px;
}

/* Body padding. The homepage holds .controls + .hint; the settings page puts
   a generated #config-fields wrapper inside. Both get the same inset so the
   content is not flush against the panel edge. */
.advanced > #config-fields {
    padding: 4px 14px 12px;
}

.hint {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin: 0;
    padding: 0 14px 12px;
    line-height: 1.45;
}

/* -------------------------
   Small screens
   ------------------------- */

@media (max-width: 480px) {
    body {
        padding:
            calc(max(0.75rem, env(safe-area-inset-top)) + 1rem)
            max(0.6rem, env(safe-area-inset-right))
            max(0.75rem, env(safe-area-inset-bottom))
            max(0.6rem, env(safe-area-inset-left));
    }

    .container {
        padding: 1.1rem 1rem;
    }

    /* Full-width stacked buttons are easiest to hit one-handed */
    .controls {
        flex-direction: column;
    }

    button {
        flex: 1 1 auto;
        width: 100%;
    }

    .status-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.15rem;
    }

    /* Too narrow for two columns - stack lat/lon instead of squeezing them */
    .field-row {
        flex-direction: column;
        gap: 0;
    }
}

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