/* ==========================================================================
   Studio Studio — Book Library, admin (backend) styles.

   Written mobile-first: the base rules are what a phone gets, the
   `min-width: 700px` blocks add the roomier desktop layout on top.
   Every admin page loads this file, so mobile fixes only have to be made once.
   ========================================================================== */

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

html {
    /* Stop iOS from inflating text when the phone is rotated to landscape. */
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
    background: #fff;
    color: #000;
    line-height: 1.45;
    max-width: 600px;
    margin: 0 auto;
    padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
    /* Long ISBNs, URLs and tokens must never push the page sideways. */
    overflow-wrap: break-word;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.08);
}

/* The dashboard needs more room for its table. */
body.admin-wide { max-width: 900px; }

a { color: #000; }

img { max-width: 100%; }

h1 {
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 24px;
}

h1 a { text-decoration: none; }

h2 {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-top: 30px;
    margin-bottom: 12px;
    border-bottom: 1px solid #000;
    padding-bottom: 6px;
}

/* Back link — padded out to a comfortable thumb target. */
.back {
    display: inline-block;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 10px 8px 10px 0;
    margin-bottom: 8px;
}

.subnav {
    font-size: 12px;
    color: #999;
    margin-bottom: 20px;
    display: flex;
    flex-wrap: wrap;
    gap: 4px 14px;
}

.subnav a {
    color: #999;
    padding: 6px 0;
    display: inline-block;
}

/* ── Messages ─────────────────────────────────────────────────────────── */

.flash { padding: 12px; border: 1px solid #000; margin-bottom: 20px; font-size: 14px; }
.error { color: #c00; font-size: 14px; margin-bottom: 10px; }
.empty { font-size: 14px; color: #666; padding: 40px 0; text-align: center; }

.warn {
    padding: 14px;
    border: 1px solid #e53935;
    background: #fce4ec;
    margin-bottom: 20px;
    font-size: 13px;
    line-height: 1.6;
}

.warn strong { display: block; font-size: 14px; }

.warn a {
    display: inline-block;
    margin-top: 8px;
    padding: 6px 0;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* ── Form controls ────────────────────────────────────────────────────── */

label {
    display: block;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 6px;
    margin-top: 20px;
}

input[type="text"],
input[type="search"],
input[type="number"],
input[type="password"],
input[type="email"],
input[type="url"],
select,
textarea {
    width: 100%;
    min-height: 44px;
    padding: 12px;
    border: 1px solid #000;
    border-radius: 0;
    background: #fff;
    color: #000;
    font-family: inherit;
    /* 16px is the threshold below which iOS Safari zooms in on focus. */
    font-size: 16px;
    line-height: 1.3;
    -webkit-appearance: none;
    appearance: none;
}

textarea { min-height: 120px; resize: vertical; }

/* Keep a focused field clear of the sticky save bar when the browser
   scrolls it into view. */
input,
select,
textarea,
.cat-dropdown-toggle { scroll-margin-bottom: 90px; }

input[type="file"] {
    font-family: inherit;
    font-size: 14px;
    margin-top: 4px;
    max-width: 100%;
}

input[type="checkbox"] {
    width: 20px;
    height: 20px;
    min-height: 0;
    accent-color: #000;
    flex-shrink: 0;
}

input[type="search"]::-webkit-search-decoration,
input[type="search"]::-webkit-search-cancel-button { -webkit-appearance: none; }

:is(input, textarea, select, button, .btn, [role="button"]):focus-visible {
    outline: 2px solid #000;
    outline-offset: 2px;
}

button,
.btn {
    display: inline-block;
    min-height: 44px;
    padding: 12px 18px;
    background: #000;
    color: #fff;
    border: none;
    border-radius: 0;
    font-family: inherit;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    text-align: center;
    text-decoration: none;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
    /* Kills the 300ms tap delay on older mobile browsers. */
    touch-action: manipulation;
}

button:hover,
.btn:hover { opacity: 0.8; }

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

/* Destructive actions read as outlines, not as the loudest thing on screen —
   which also makes them harder to hit by accident with a thumb. */
.btn-delete { background: #fff; color: #c00; border: 1px solid #c00; }

/* Full-width buttons read better than tiny ones on a phone. */
.btn-block { display: block; width: 100%; }

.form-error { color: #c00; font-size: 13px; margin-top: 4px; display: none; }

/* Two or three fields side by side on desktop, stacked on a phone. */
.row { display: flex; flex-direction: column; gap: 0; }
.row > div { flex: 1; min-width: 0; }

/* Input + button on one line (ISBN search, add category, rename). */
.input-row { display: flex; gap: 8px; align-items: stretch; }
.input-row input { flex: 1; min-width: 0; }
.input-row button { flex-shrink: 0; white-space: nowrap; }

/* The save button stays reachable at the bottom of long forms on mobile. */
.form-actions {
    position: sticky;
    bottom: 0;
    z-index: 15;
    margin-top: 24px;
    padding-top: 10px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    background: #fff;
    box-shadow: 0 -10px 14px -10px rgba(0, 0, 0, 0.3);
}

.btn-submit {
    display: block;
    width: 100%;
    margin-top: 0;
    padding: 16px 20px;
    font-size: 13px;
}

.checkbox-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 24px;
    padding: 6px 0;
    cursor: pointer;
}

.checkbox-row input { width: 20px; }
.checkbox-row span { font-size: 15px; text-transform: none; letter-spacing: 0; }

/* ── Category multi-select (add / edit) ───────────────────────────────── */

.cat-dropdown { position: relative; margin-top: 4px; }

.cat-dropdown-toggle {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    min-height: 44px;
    padding: 12px;
    border: 1px solid #000;
    background: #fff;
    font-size: 16px;
    cursor: pointer;
}

.cat-dropdown-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-dropdown-arrow { font-size: 10px; flex-shrink: 0; }

.cat-dropdown-menu {
    display: none;
    position: absolute;
    left: 0;
    right: 0;
    top: 100%;
    z-index: 20;
    max-height: 50vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    background: #fff;
    border: 1px solid #000;
    border-top: none;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.cat-dropdown.open .cat-dropdown-menu { display: block; }
.cat-dropdown.open .cat-dropdown-arrow { transform: rotate(180deg); }

.cat-dropdown-item {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 44px;
    margin: 0;
    padding: 10px 12px;
    border-bottom: 1px solid #f0f0f0;
    font-size: 15px;
    text-transform: none;
    letter-spacing: 0;
    cursor: pointer;
}

.cat-dropdown-item:hover { background: #f5f5f5; }
.cat-dropdown-item input[type="checkbox"] { margin: 0; cursor: pointer; }

.cat-dropdown-new { border-top: 1px dashed #ccc; }

.cat-dropdown-new input {
    width: 100%;
    min-height: 44px;
    padding: 12px;
    border: none;
    font-size: 16px;
    font-family: inherit;
    outline: none;
}

/* ── Colour picker ────────────────────────────────────────────────────── */

.color-picker { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

.color-pick {
    display: block;
    width: 40px;
    height: 40px;
    border: 1px solid #000;
    cursor: pointer;
    touch-action: manipulation;
}

.color-pick.selected { outline: 2px solid #000; outline-offset: 2px; }

/* ── Cover, camera and scanner ────────────────────────────────────────── */

.cover-preview,
.current-cover { margin-top: 10px; max-width: 200px; }

.cover-preview img,
.current-cover img { width: 100%; border: 1px solid #000; }

#scanner-container { display: none; margin-top: 10px; margin-bottom: 16px; border: 1px solid #000; }
#scanner-container #reader { width: 100%; }
#scanner-status { font-size: 13px; margin-top: 8px; color: #666; line-height: 1.5; }

#cover-camera-container { display: none; margin-top: 10px; border: 1px solid #000; background: #000; }
#cover-camera-video { width: 100%; display: none; }
#cover-crop-area { display: none; }
#cover-crop-area img { max-width: 100%; display: block; }

.cover-camera-buttons,
#cover-camera-buttons-crop { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
#cover-camera-buttons-crop { display: none; }

.cover-camera-buttons button,
#cover-camera-buttons-crop button { flex: 1 1 130px; }

#cover-camera-preview { display: none; margin-top: 10px; max-width: 200px; }
#cover-camera-preview img { width: 100%; border: 1px solid #000; }

/* ── Dashboard ────────────────────────────────────────────────────────── */

.admin-bar { display: flex; justify-content: space-between; align-items: center; gap: 12px; }

.logout-form { display: inline; }

.logout-btn {
    min-height: 0;
    padding: 6px 0;
    background: none;
    border: none;
    color: inherit;
    font-size: 11px;
    letter-spacing: 0.05em;
    text-decoration: underline;
    cursor: pointer;
}

.logout-btn:hover { opacity: 0.6; }

.stats {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #666;
    margin-bottom: 16px;
}

.actions {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    margin-bottom: 20px;
}

.search-form { display: flex; flex-wrap: wrap; gap: 8px; }
.search-form input { flex: 1 1 150px; min-width: 0; }
.search-form button,
.search-form .btn { flex: 0 0 auto; }

.book-table { width: 100%; border-collapse: collapse; font-size: 14px; }

.book-table th {
    text-align: left;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 8px 10px;
    border-bottom: 2px solid #000;
}

.book-table td { padding: 10px; border-bottom: 1px solid #ddd; vertical-align: middle; }

.color-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border: 1px solid #000;
    margin-right: 6px;
    vertical-align: middle;
    flex-shrink: 0;
}

.date { font-size: 12px; color: #666; white-space: nowrap; }

.row-actions { display: flex; gap: 8px; align-items: center; }

.row-actions a {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 8px 0;
}

.row-actions .del { color: #c00; }

/* Inline category editor on the dashboard. */
.cat-cell { position: relative; }

.cat-display {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 0;
    padding: 6px 0;
    background: none;
    border: none;
    color: inherit;
    font-family: inherit;
    font-size: 14px;
    text-transform: none;
    letter-spacing: 0;
    text-align: left;
    cursor: pointer;
}

.cat-display::after { content: '▾'; font-size: 9px; color: #999; }
.cat-display:hover { opacity: 1; text-decoration: underline; }
.cat-display .none { color: #999; font-style: italic; }

.cat-edit {
    display: none;
    position: absolute;
    left: 0;
    top: 100%;
    z-index: 30;
    min-width: 200px;
    max-height: 50vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    background: #fff;
    border: 1px solid #000;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
}

.cat-check {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 44px;
    margin: 0;
    padding: 10px 12px;
    font-size: 15px;
    text-transform: none;
    letter-spacing: 0;
    white-space: nowrap;
    cursor: pointer;
}

.cat-check:hover { background: #f5f5f5; }
.cat-check input { margin: 0; cursor: pointer; }

.cat-new-input {
    width: 100%;
    min-height: 44px;
    padding: 12px;
    border: none;
    border-top: 1px dashed #ccc;
    font-family: inherit;
    font-size: 16px;
    outline: none;
}

.cat-cell.open .cat-edit { display: block; }

.cat-cell.saved .cat-display { color: #090; }
.cat-cell.error .cat-display { color: #c00; }

/* Phone: turn each table row into a self-contained card. */
@media (max-width: 699px) {
    .book-table thead {
        position: absolute;
        width: 1px;
        height: 1px;
        margin: -1px;
        padding: 0;
        overflow: hidden;
        clip: rect(0 0 0 0);
        border: 0;
    }

    .book-table,
    .book-table tbody,
    .book-table tr,
    .book-table td { display: block; width: 100%; }

    .book-table tr {
        border: 1px solid #000;
        padding: 14px;
        margin-bottom: 12px;
    }

    .book-table td { border: none; padding: 0; }

    .col-title { font-size: 16px; font-weight: 600; line-height: 1.3; }
    .col-author { font-size: 14px; color: #444; margin-top: 2px; }
    .cat-cell { margin-top: 8px; }
    .col-date { margin-top: 6px; }

    .cat-edit { right: 0; min-width: 0; }

    .col-actions { margin-top: 14px; }

    .row-actions a {
        flex: 1;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 10px;
        border: 1px solid #000;
        text-decoration: none;
    }

    .row-actions .del { border-color: #c00; }
}

/* ── Login ────────────────────────────────────────────────────────────── */

body.admin-login {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    min-height: 100svh;
    padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
}

.login { width: 100%; max-width: 360px; }
.login h1 { margin-bottom: 24px; }
.login .btn-submit { margin-top: 24px; }

/* ── Categories page ──────────────────────────────────────────────────── */

.add-form { display: flex; gap: 8px; align-items: stretch; margin-bottom: 24px; }
.add-form input { flex: 1; min-width: 0; }
.add-form button { flex-shrink: 0; }

.category-item { border-bottom: 1px solid #ddd; padding: 14px 0; }

.category-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 12px;
}

.category-name { font-size: 16px; font-weight: 600; }

.category-count {
    font-size: 11px;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    white-space: nowrap;
}

.category-actions { display: flex; gap: 8px; margin-top: 10px; }
.category-actions form { flex: 1; display: flex; }
.category-actions button { flex: 1; padding: 10px 12px; font-size: 11px; }

.rename-form { display: none; margin-top: 12px; }
.rename-form .rename-row { display: flex; flex-wrap: wrap; gap: 8px; }
.rename-form input { flex: 1 1 100%; }
.rename-form button { flex: 1; }

/* ── Instagram settings ───────────────────────────────────────────────── */

.message { padding: 12px; font-size: 13px; margin-bottom: 16px; line-height: 1.5; }
.message.success { background: #e8f5e9; border: 1px solid #4caf50; }
.message.error { background: #fce4ec; border: 1px solid #e53935; }

.status-item {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 4px 12px;
    padding: 10px 0;
    border-bottom: 1px solid #eee;
    font-size: 13px;
}

.status-label {
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-size: 10px;
    color: #666;
}

.ok { color: #4caf50; }
.missing { color: #e53935; }
.hint { font-size: 12px; color: #666; margin-top: 6px; line-height: 1.5; }

.toggle { display: flex; align-items: center; gap: 10px; margin-top: 16px; padding: 6px 0; cursor: pointer; }
.toggle input { width: auto; }
.toggle span { font-size: 15px; text-transform: none; letter-spacing: 0; }

.test-card { margin-top: 12px; max-width: 300px; border: 1px solid #000; }
.test-card img { width: 100%; display: block; }

.section { background: #f9f9f9; padding: 16px; margin-top: 16px; border: 1px solid #eee; }
.section ol,
.steps { font-size: 12px; color: #666; margin-left: 18px; margin-bottom: 12px; line-height: 1.8; }

.alert { background: #fce4ec; border: 1px solid #e53935; padding: 14px; margin-top: 16px; font-size: 13px; line-height: 1.6; }
.alert strong { display: block; font-size: 14px; }

.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

/* ── Diagnostics ──────────────────────────────────────────────────────── */

.item { padding: 12px 0; border-bottom: 1px solid #ddd; font-size: 14px; }
.item .label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: #666; margin: 0 0 2px; }
.fail { color: #c00; }

/* ==========================================================================
   Desktop — restore the tighter, denser layout on larger screens.
   ========================================================================== */

@media (min-width: 700px) {
    body { padding: 30px 20px; }

    h1 { margin-bottom: 30px; }

    input[type="text"],
    input[type="search"],
    input[type="number"],
    input[type="password"],
    input[type="email"],
    input[type="url"],
    select,
    textarea {
        min-height: 0;
        padding: 10px;
        font-size: 13px;
    }

    textarea { min-height: 100px; }

    button,
    .btn { min-height: 0; padding: 12px 20px; font-size: 11px; }

    .btn-block { display: inline-block; width: auto; }

    .row { flex-direction: row; gap: 12px; }

    .actions { flex-direction: row; align-items: center; }
    .actions .search-form { flex: 1; }

    .form-actions {
        position: static;
        padding: 0;
        margin-top: 30px;
        box-shadow: none;
    }

    .cat-dropdown-toggle { min-height: 0; padding: 10px; font-size: 13px; }
    .cat-dropdown-item { min-height: 0; padding: 8px 10px; font-size: 13px; }
    .cat-dropdown-new input { min-height: 0; padding: 8px 10px; font-size: 13px; }

    .cat-check { min-height: 0; padding: 6px 10px; font-size: 12px; }
    .cat-new-input { min-height: 0; padding: 6px 10px; font-size: 12px; }
    .cat-display { font-size: 12px; padding: 4px 0; }

    .color-picker { gap: 4px; }
    .color-pick { width: 28px; height: 28px; }
    .color-pick:hover { transform: scale(1.15); }

    .book-table { font-size: 13px; }
    .book-table td { padding: 8px 10px; }

    .row-actions a { padding: 4px 0; }

    .category-actions { display: inline-flex; }
    .category-actions form { flex: 0 0 auto; }
    .category-actions button { flex: 0 0 auto; padding: 8px 14px; font-size: 10px; }

    .rename-form input { flex: 1 1 auto; }
    .rename-form button { flex: 0 0 auto; }

    .checkbox-row span,
    .toggle span { font-size: 13px; }

    .category-name { font-size: 14px; }
}

/* Touch devices wide enough for the desktop layout (a phone in landscape,
   a tablet) still need thumb-sized targets. */
@media (pointer: coarse) {
    .row-actions a { padding: 12px 0; }
    .row-actions { gap: 16px; }
    .back { padding: 12px 8px 12px 0; }
    .cat-check { min-height: 44px; padding: 10px 12px; }
    .cat-dropdown-item { min-height: 44px; }
}

/* Coarse pointers (touch) never benefit from hover-only affordances. */
@media (hover: none) {
    button:hover,
    .btn:hover,
    .color-pick:hover,
    .cat-check:hover,
    .cat-dropdown-item:hover { opacity: 1; transform: none; background-color: inherit; }

    .cat-check:hover,
    .cat-dropdown-item:hover { background: #fff; }
}
