#settings {
    position: fixed;
    top: 0;
    left: 0;
    width: 100svw;
    height: 100svh;
    background: var(--background);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: top 0.4s ease;
    box-shadow: 0 0 2rem var(--shadow-lg);
}

#settings * {
    box-sizing: border-box;
}

#settings > .page {
    height: 100svh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: 0.5rem 1rem;
    max-width: 1200px;
    margin: 0 auto;
    width: 100%;
    /* Only the content area should scroll; keep tabs + actions always visible */
    overflow: hidden;
}

/* Welcome/Required Message */
.settings-required-message {
    width: 100%;
    max-width: 600px;
    margin: 2rem auto;
    padding: 2rem;
    background: var(--surface);
    border-radius: var(--border-radius);
    box-shadow: 0 4px 6px var(--shadow);
    border: 2px solid var(--primary);
}

.required-message-content h2 {
    color: var(--primary);
    margin-bottom: 1rem;
    font-size: 1.5rem;
}

.required-message-content p {
    color: var(--text-primary);
    margin-bottom: 1rem;
}

.required-message-content ul {
    list-style: none;
    padding-left: 0;
}

.required-message-content li {
    padding: 0.5rem 0;
    color: var(--text-secondary);
    padding-left: 1.5rem;
    position: relative;
}

.required-message-content li:before {
    content: "✓";
    position: absolute;
    left: 0;
    color: var(--primary);
    font-weight: bold;
}

/* Validation Errors */
.validation-errors {
    width: 100%;
    max-width: 600px;
    margin: 1rem auto;
    padding: 1rem;
    background: var(--error);
    color: white;
    border-radius: var(--border-radius);
    box-shadow: 0 2px 4px var(--shadow);
}

.error-content ul {
    margin: 0.5rem 0 0 0;
    padding-left: 1.5rem;
}

.error-content li {
    margin: 0.25rem 0;
}

/* Settings Tabs */
#settings .page input[type="radio"][name="settings"] {
    display: none;
}

#settings .tabs {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.35rem;
    padding: 0.5rem;
    background: var(--glass-bg);
    backdrop-filter: blur(var(--glass-blur));
    -webkit-backdrop-filter: blur(var(--glass-blur));
    border-radius: var(--border-radius);
    border: 1px solid var(--glass-border);
    flex: 0 0 auto;
}

#settings .tabs > label {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.6rem 0.75rem;
    text-align: center;
    font-weight: 600;
    cursor: pointer;
    border-radius: calc(var(--border-radius) - 2px);
    background: transparent;
    color: var(--text-secondary);
    transition: var(--transition);
    border: 1px solid transparent;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.9rem;
}

#settings .tabs > label:hover {
    background: var(--surface-hover);
    color: var(--text-primary);
}

/* Active tab styling without :has() */
#settings-1:checked ~ .tabs label[for="settings-1"],
#settings-2:checked ~ .tabs label[for="settings-2"],
#settings-3:checked ~ .tabs label[for="settings-3"] {
    background: var(--surface-solid);
    color: var(--accent-dark);
    border-color: var(--border);
    box-shadow: 0 2px 8px var(--shadow);
    font-weight: 700;
}

/* Page Content */
#settings .contents {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    /* Scroll area lives between tabs and actions */
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding-bottom: 0.75rem;
}

#settings .page-content {
    background: var(--glass-bg);
    backdrop-filter: blur(var(--glass-blur));
    -webkit-backdrop-filter: blur(var(--glass-blur));
    display: none;
    flex-direction: column;
    width: 100%;
    max-width: 600px;
    margin: 0.5rem auto;
    padding: 1rem 1.5rem;
    border-radius: var(--border-radius);
    border: 1px solid var(--glass-border);
    box-shadow: 0 4px 16px var(--shadow);
    gap: 1rem;
}

#settings .page-content > h2 {
    margin: 0.5rem 0 0.25rem 0;
    font-size: 1.1rem;
    color: var(--text-primary);
    font-weight: bold;
}

#settings-1:checked ~ .contents #settings-content-1,
#settings-2:checked ~ .contents #settings-content-2,
#settings-3:checked ~ .contents #settings-content-3 {
    display: flex;
}

/* Setting Items */
.setting {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding: 0.75rem;
    background: var(--surface);
    border-radius: var(--border-radius);
    border: 1px solid var(--border);
    transition: var(--transition);
    position: relative;
}

.setting:hover {
    border-color: var(--accent);
    box-shadow: 0 2px 8px var(--shadow);
    background: var(--surface-hover);
}

.setting > label {
    font-weight: bold;
    color: var(--text-primary);
    font-size: 0.95rem;
    margin-bottom: 0.25rem;
}

.setting.required > label::after {
    content: "Required";
    display: inline-block;
    margin-left: 0.5rem;
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    background: rgba(46, 204, 64, 0.12);
    color: var(--accent-dark);
    border: 1px solid rgba(46, 204, 64, 0.25);
    vertical-align: middle;
}

.setting.required.missing {
    border-color: var(--error);
    box-shadow: 0 2px 8px rgba(211, 47, 47, 0.15);
}

.setting.required.missing > label::after {
    background: var(--error);
    color: white;
    border-color: rgba(0, 0, 0, 0.12);
}

.setting > input[type="text"],
.setting > input[type="number"],
.setting > select {
    padding: 0.6rem;
    border: 2px solid var(--border);
    border-radius: var(--border-radius);
    background: var(--background);
    color: var(--text-primary);
    font-size: 0.95rem;
    transition: var(--transition);
    width: 100%;
}

.setting > input:focus,
.setting > select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(46, 204, 64, 0.12);
}

.setting > input.valid {
    border-color: var(--success);
    background-color: rgba(127, 207, 33, 0.05);
}

.setting > input.invalid {
    border-color: var(--error);
    background-color: rgba(211, 47, 47, 0.05);
}

.setting > input:disabled {
    background: var(--surface);
    color: var(--text-secondary);
    cursor: not-allowed;
}

.setting > button {
    padding: 0.6rem 1.25rem;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--border-radius);
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    font-size: 0.95rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.setting > button:hover {
    background: var(--accent-dark);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px var(--shadow);
}

.setting > button:active {
    transform: translateY(0);
}

.setting > button > .icon {
    height: 1em;
    width: 1em;
}

.setting > #cancel {
    background: var(--surface);
    color: var(--text-secondary);
    border: 1px solid var(--border);
}

.setting > #cancel:hover {
    background: var(--surface-hover);
    color: var(--text-primary);
}

.setting > #close {
    background: var(--accent);
    color: #fff;
    border: none;
}

.setting > #close:hover {
    background: var(--accent-dark);
}

.setting > input[type="checkbox"] {
    width: 1.5rem;
    height: 1.5rem;
    cursor: pointer;
    accent-color: var(--accent);
}

/* Info Tooltips */
.setting > .info {
    display: none;
    position: absolute;
    background: var(--surface-solid);
    border: 1px solid var(--border);
    border-radius: var(--border-radius);
    padding: 1rem;
    box-shadow: 0 4px 16px var(--shadow-lg);
    z-index: 10;
    max-width: 320px;
    margin-top: 0.5rem;
    left: 0;
    top: calc(100% - 0.25rem);
}

.setting > label:hover + .info,
.setting > label:focus + .info,
.setting > button:hover + .info {
    display: block;
}

.setting > .info > p {
    margin: 0;
    color: var(--text-primary);
    font-size: 0.85rem;
    line-height: 1.4;
}

.setting.required > .info > p {
    font-size: 0.8rem;
    line-height: 1.25;
}

.setting > .info > p:before {
    content: 'ℹ️ ';
    margin-right: 0.5rem;
}

.setting.hidden {
    display: none;
}

/* Inline rows used in some settings */
.setting-row {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-wrap: wrap;
}

.setting-row > input,
.setting-row > select {
    flex: 1 1 240px;
    min-width: 160px;
}

.setting-row > button {
    flex: 0 0 auto;
}

/* GPS row in Grid Square setting: button + lat + lon on one line */
.gps-row {
    display: flex;
    gap: 0.5rem;
    align-items: stretch;
    flex-wrap: nowrap;
    width: 100%;
}

.gps-row > button {
    flex: 0 0 auto;
    padding: 0.6rem 0.9rem;
}

.gps-coord {
    flex: 1 1 auto;
    min-width: 0;
    max-width: none;
    text-align: right;
    font-variant-numeric: tabular-nums;
    padding: 0.6rem;
}

/* GPS Coordinates */
.GPS-Coordinates {
    display: flex;
    gap: 1rem;
    flex-direction: row;
    flex-wrap: wrap;
}

.GPS-Coordinates > input {
    flex: 1;
    min-width: 150px;
}

/* Action Buttons */
#settings .settings-actions {
    /* Less "box-in-a-box": keep actions compact */
    display: flex;
    flex-direction: row;
    gap: 0.75rem;
    justify-content: space-between;
    margin-top: 0.5rem;
    padding: 0.5rem 0;
    background: transparent;
    border: 0;
    box-shadow: none;
    flex-shrink: 0;
    flex: 0 0 auto;
    width: 100%;
    max-width: 600px;
}

#settings .settings-actions > #cancel,
#settings .settings-actions > #close {
    flex: 1;
    padding: 0.7rem 0.9rem;
    font-size: 0.95rem;
}

/* Responsive Design */
@media (max-width: 768px) {
    #settings > .page {
        padding: 0.5rem;
    }

    #settings .tabs {
        gap: 0.4rem;
    }

    #settings .tabs > label {
        padding: 0.6rem 0.5rem;
        font-size: 0.85rem;
        border-width: 1px;
    }

    #settings .page-content {
        padding: 1rem;
    }

    .settings-required-message {
        padding: 1.5rem;
        margin: 1rem;
    }

    .GPS-Coordinates {
        flex-direction: column;
    }

    .GPS-Coordinates > input {
        width: 100%;
    }

    #settings .settings-actions {
        flex-direction: column;
        gap: 0.5rem;
    }

    #settings .settings-actions > #cancel,
    #settings .settings-actions > #close {
        width: 100%;
    }

    /* On touch/mobile, show help text inline instead of hover tooltips */
    .setting > .info {
        display: block;
        position: static;
        border: 0;
        box-shadow: none;
        padding: 0;
        margin-top: 0.15rem;
        max-width: none;
        background: transparent;
        opacity: 0.9;
    }

    .setting.required > .info > p {
        font-size: 0.75rem;
        line-height: 1.2;
    }
}

@media (max-width: 480px) {
    #settings .tabs > label {
        padding: 0.55rem 0.35rem;
        font-size: 0.8rem;
    }

    .settings-required-message {
        padding: 1rem;
    }

    .required-message-content h2 {
        font-size: 1.25rem;
    }
}

/* Dark theme adjustments */
[data-theme="ribbit-dark"] .setting,
[data-theme="auto"] .setting {
    background: var(--surface);
}

[data-theme="ribbit-dark"] .setting > input,
[data-theme="ribbit-dark"] .setting > select,
[data-theme="auto"] .setting > input,
[data-theme="auto"] .setting > select {
    background: var(--background-solid);
    color: var(--text-primary);
}
