:root {
    --bg: #f5f6f8;
    --card: #ffffff;
    --text: #1c1f23;
    --muted: #6b7280;
    --border: #e2e5ea;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --error: #dc2626;
    --success: #16a34a;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14161a;
        --card: #1e2126;
        --text: #e7e9ee;
        --muted: #9aa1ad;
        --border: #2c3038;
        --primary: #3b82f6;
        --primary-hover: #60a5fa;
        --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    }
}

* { box-sizing: border-box; }

/* The [hidden] attribute must always win over the display rules below. */
[hidden] { display: none !important; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 24px;
    background: var(--card);
    border-bottom: 1px solid var(--border);
}

.topbar h1 { font-size: 18px; margin: 0; }

.topbar-right { display: flex; align-items: center; gap: 12px; }

.user-label { color: var(--muted); font-size: 14px; }

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: var(--shadow);
    padding: 20px;
}

/* Login */
.login-view {
    display: flex;
    align-items: center;
    justify-content: center;
    /* Fill the viewport below the header and center the card in it. */
    min-height: calc(100vh - 56px);
    min-height: calc(100dvh - 56px);
    padding: 24px 16px;
}

.login-card {
    width: 100%;
    max-width: 360px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.login-card h2 { margin: 0 0 4px; }

label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 14px;
    color: var(--muted);
}

input[type="text"],
input[type="password"],
input[type="search"] {
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
}

input:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

.upload-row input.invalid {
    border-color: var(--error);
    color: var(--error);
}
.upload-row input.invalid:focus { outline-color: var(--error); }

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.hint {
    font-size: 12px;
    color: var(--muted);
    margin: 0;
}
ul.hint {
    padding-left: 16px;
    line-height: 1.45;
}
ul.hint li { margin: 0; }
.hint.invalid-hint { color: var(--error); }

/* Buttons */
.btn {
    padding: 10px 16px;
    border-radius: 8px;
    border: 1px solid transparent;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s ease;
}

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }

.btn-ghost {
    background: transparent;
    color: var(--text);
    border-color: var(--border);
}
.btn-ghost:hover { background: var(--bg); }

/* Main */
.app-view {
    max-width: 1080px;
    margin: 24px auto;
    padding: 0 16px;
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.upload-card h2 { margin: 0 0 14px; }

.upload-row {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 12px;
}

/* Name input + its rules, stacked so the rules sit under the input. */
.name-field {
    flex: 1;
    min-width: 200px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.name-field input[type="text"] { width: 100%; }

/* Keep single-line controls aligned to the input line. */
.file-input,
.upload-row > .checkbox,
.upload-row > .btn { min-height: 40px; }
.upload-row > .checkbox { align-items: center; }

/* Custom file picker: hide the native input, use the label as the button. */
.file-input {
    cursor: pointer;
    flex: 0 0 auto;
}

.file-input input[type="file"] {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.file-name {
    font-size: 13px;
    color: var(--muted);
    display: inline-flex;
    align-items: center;
    min-height: 40px;
    word-break: break-all;
    max-width: 240px;
}

.checkbox {
    flex-direction: row;
    align-items: center;
    gap: 6px;
    color: var(--text);
}

.gallery-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.gallery-header h2 { margin: 0; }

.gallery-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

#searchInput { min-width: 200px; }

.view-toggle { display: flex; gap: 4px; }

.btn.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

/* Table view */
.img-table {
    width: 100%;
    border-collapse: collapse;
}

.img-table th,
.img-table td {
    text-align: left;
    padding: 8px 10px;
    border-bottom: 1px solid var(--border);
    font-size: 14px;
}

.img-table th {
    color: var(--muted);
    font-weight: 500;
}

.img-table td.col-actions {
    text-align: right;
    width: 1%;
    white-space: nowrap;
}

.img-table a {
    color: var(--primary);
    text-decoration: none;
    word-break: break-all;
}

.img-table a:hover { text-decoration: underline; }

.gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 16px;
}

.tile {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: var(--shadow);
}

.tile img {
    width: 100%;
    height: 150px;
    object-fit: cover;
    display: block;
    background: var(--bg);
}

.tile-meta {
    padding: 8px 10px;
    font-size: 12px;
}

.tile-name {
    font-weight: 500;
    word-break: break-all;
}

.tile-sub { color: var(--muted); margin-top: 2px; }

.tile-actions {
    display: flex;
    gap: 6px;
    margin-top: 8px;
}

.btn-sm {
    padding: 5px 10px;
    font-size: 12px;
    border-radius: 6px;
    flex: 1;
    text-align: center;
}

.empty {
    color: var(--muted);
    padding: 32px;
    text-align: center;
}

/* Status messages */
.status, .error { font-size: 14px; margin: 12px 0 0; }
.error { color: var(--error); }
.status.ok { color: var(--success); }
.status.err { color: var(--error); }

/* Modal */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    z-index: 100;
}

.modal {
    position: relative;
    width: 100%;
    max-width: 520px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.modal-close {
    position: absolute;
    top: 10px;
    right: 12px;
    background: transparent;
    border: none;
    color: var(--muted);
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 6px;
}
.modal-close:hover { color: var(--text); background: var(--bg); }

.modal h2 { margin: 0; }
.modal-sub { color: var(--muted); margin: 0; font-size: 14px; }

.url-box { display: flex; }
.url-box input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-size: 13px;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.modal-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}
.modal-actions .btn { flex: 1; min-width: 110px; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; }

/* Mobile */
@media (max-width: 600px) {
    .topbar { padding: 12px 16px; }
    .topbar h1 { font-size: 16px; }

    .app-view { margin: 16px auto; padding: 0 12px; gap: 16px; }
    .card { padding: 16px; }

    .upload-row .btn,
    .file-input { width: 100%; }
    .name-field { min-width: 0; }

    .gallery {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        gap: 12px;
    }

    .gallery-controls { width: 100%; }
    #searchInput { flex: 1; min-width: 0; }

    .modal-actions .btn { min-width: 0; }
}
