:root {
    --bg: #f5f4f2;
    --card-bg: #ffffff;
    --text: #2a2724;
    --muted: #7a746c;
    --border: #e4e0da;
    --accent: #8a5a3b;
    --accent-hover: #6f4730;
    --danger: #b6452c;
}

* {
    box-sizing: border-box;
}

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

header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.25rem 2rem;
    background: var(--card-bg);
    border-bottom: 1px solid var(--border);
}

header h1 {
    margin: 0;
    font-size: 1.4rem;
}

.app-header {
    display: grid;
    grid-template-columns: max-content minmax(220px, 560px) max-content;
    justify-content: space-between;
    gap: 1rem;
}

.app-header h1 {
    white-space: nowrap;
}

.header-search {
    position: relative;
    display: flex;
    width: 100%;
    min-width: 0;
    justify-self: center;
    grid-column: 2;
    grid-row: 1;
}

.app-header > .btn {
    grid-column: 3;
    grid-row: 1;
    white-space: nowrap;
}

.header-search input {
    width: 100%;
    min-width: 0;
    height: 42px;
    padding: 0.65rem 5rem 0.65rem 2.5rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--bg);
    color: var(--text);
    font: inherit;
    font-size: 16px;
    outline: none;
}

.header-search input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(138, 90, 59, 0.14);
}

.header-search button {
    position: absolute;
    top: 4px;
    right: 4px;
    height: 34px;
    padding: 0 0.9rem;
    border: 0;
    border-radius: 999px;
    background: var(--accent);
    color: #fff;
    font: inherit;
    cursor: pointer;
}

.search-icon {
    position: absolute;
    top: 12px;
    left: 14px;
    z-index: 1;
    width: 15px;
    height: 15px;
    border: 2px solid var(--muted);
    border-radius: 50%;
    pointer-events: none;
}

.search-icon::after {
    content: "";
    position: absolute;
    width: 7px;
    height: 2px;
    right: -5px;
    bottom: -3px;
    background: var(--muted);
    transform: rotate(45deg);
    border-radius: 2px;
}

.btn {
    display: inline-block;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    border: 1px solid var(--accent);
    background: var(--accent);
    color: #fff;
    text-decoration: none;
    font-size: 0.9rem;
    cursor: pointer;
}

.btn:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
}

.btn-secondary {
    background: transparent;
    color: var(--accent);
}

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

.btn-danger {
    background: transparent;
    color: var(--danger);
    border-color: var(--danger);
}

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

main {
    max-width: 1100px;
    margin: 0 auto;
    padding: 1.5rem 2rem 3rem;
}

.filters {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
}

.filters select {
    padding: 0.4rem 0.6rem;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--card-bg);
}

.flash {
    padding: 0.6rem 1rem;
    border-radius: 6px;
    background: #e8f3e8;
    color: #2f5c2f;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}

.empty {
    text-align: center;
    color: var(--muted);
    padding: 3rem 0;
}

.results-summary {
    margin: -0.5rem 0 1rem;
    color: var(--muted);
    font-size: 0.9rem;
}

.offline-page {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    text-align: center;
}

.offline-page h1,
.offline-page p {
    margin: 0;
}

.offline-page p {
    color: var(--muted);
}

@media (max-width: 900px) {
    .app-header {
        grid-template-columns: 1fr auto;
    }

    .header-search {
        grid-column: 1 / -1;
        grid-row: 2;
        max-width: none;
    }

    .app-header > .btn {
        grid-column: 2;
        grid-row: 1;
    }
}

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 1.25rem;
}

.card {
    position: relative;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: visible;
    display: flex;
    flex-direction: column;
}

.card-photo {
    border-radius: 10px 10px 0 0;
    overflow: hidden;
}

.card-menu {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    z-index: 5;
}

.menu-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.9rem;
    height: 1.9rem;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.85);
    color: var(--text);
    font-size: 1.1rem;
    line-height: 1;
    cursor: pointer;
    list-style: none;
    user-select: none;
}

.menu-toggle::-webkit-details-marker {
    display: none;
}

.menu-toggle::marker {
    content: "";
}

.menu-toggle:hover {
    background: #fff;
}

.menu-list {
    position: absolute;
    top: calc(100% + 0.3rem);
    right: 0;
    min-width: 160px;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.menu-list a,
.menu-list button {
    display: block;
    width: 100%;
    padding: 0.55rem 0.9rem;
    border: none;
    background: none;
    text-align: left;
    font-size: 0.88rem;
    font-family: inherit;
    color: var(--text);
    text-decoration: none;
    cursor: pointer;
}

.menu-list form {
    margin: 0;
}

.menu-list a:hover,
.menu-list button:hover {
    background: var(--bg);
}

.menu-list button.danger {
    color: var(--danger);
}

.card-photo {
    width: 100%;
    aspect-ratio: 3 / 4;
    background: #eee;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.card-photo img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.card-photo.placeholder {
    color: var(--muted);
    font-size: 0.85rem;
}

.card-body {
    padding: 0.85rem 1rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    flex: 1;
}

.card-body h3 {
    margin: 0;
    font-size: 1.05rem;
}

.badges {
    display: flex;
    gap: 0.4rem;
    flex-wrap: wrap;
}

.badge {
    font-size: 0.75rem;
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    background: #f0e8e0;
    color: var(--accent-hover);
}

.card-meta {
    font-size: 0.82rem;
    color: var(--muted);
}

form.item-form {
    max-width: 560px;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.field label {
    font-size: 0.9rem;
    font-weight: 600;
}

.field input[type="text"],
.field select,
.field textarea,
.field input[type="file"] {
    padding: 0.5rem 0.6rem;
    border-radius: 6px;
    border: 1px solid var(--border);
    font-size: 0.95rem;
    font-family: inherit;
}

.field textarea {
    resize: vertical;
    min-height: 70px;
}

.photo-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.6rem;
}

.photo-button {
    text-align: center;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.photo-hint,
.photo-preview span {
    color: var(--muted);
    font-size: 0.82rem;
}

.photo-preview {
    align-items: center;
    gap: 0.7rem;
    margin-top: 0.3rem;
}

.photo-preview:not([hidden]) {
    display: flex;
}

.photo-preview img {
    width: 90px;
    height: 90px;
    border-radius: 8px;
    border: 1px solid var(--border);
    object-fit: cover;
}

.error {
    color: var(--danger);
    font-size: 0.82rem;
}

.current-photo img {
    max-width: 140px;
    border-radius: 8px;
    border: 1px solid var(--border);
    display: block;
    margin-top: 0.3rem;
}

.form-actions {
    display: flex;
    gap: 0.6rem;
    margin-top: 0.5rem;
}

@media (max-width: 640px) {
    header {
        padding: 1rem;
        flex-wrap: wrap;
        gap: 0.6rem;
    }

    header h1 {
        font-size: 1.2rem;
    }

    .app-header {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
    }

    .app-header > .btn {
        grid-column: 2;
        grid-row: 1;
        white-space: nowrap;
    }

    .header-search {
        grid-column: 1 / -1;
        width: 100%;
    }

    main {
        padding: 1rem 1rem 2rem;
    }

    .photo-actions .photo-button {
        flex: 1 1 100%;
        padding: 0.75rem 1rem;
    }

    .filters {
        gap: 0.5rem;
    }

    .filters select,
    .filters a {
        flex: 1 1 auto;
    }

    .grid {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
        gap: 0.85rem;
    }

    .card-body {
        padding: 0.7rem 0.8rem 0.8rem;
        gap: 0.3rem;
    }

    .card-body h3 {
        font-size: 0.95rem;
    }

    form.item-form {
        max-width: 100%;
        padding: 1rem;
        border-radius: 0;
        border-left: none;
        border-right: none;
    }

    .form-actions {
        flex-direction: column;
    }

    .form-actions .btn {
        text-align: center;
        width: 100%;
    }
}

@media (max-width: 400px) {
    .grid {
        grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    }
}

/*
 * Some Android browsers request a desktop-sized (about 980px) viewport even
 * on a phone. Detect the touch-first device as well, so the layout still uses
 * the available screen instead of retaining desktop card/form widths.
 */
@media (hover: none) and (pointer: coarse) {
    header {
        padding: 1rem;
        flex-wrap: wrap;
        gap: 0.75rem;
    }

    header h1 {
        font-size: 1.25rem;
    }

    .app-header {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
    }

    .app-header > .btn {
        grid-column: 2;
        grid-row: 1;
        white-space: nowrap;
    }

    .header-search {
        grid-column: 1 / -1;
        grid-row: 2;
        width: 100%;
    }

    main {
        width: 100%;
        max-width: none;
        padding: 1rem;
    }

    .filters {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.75rem;
    }

    .filters select,
    .filters a {
        width: 100%;
        min-height: 44px;
        font-size: 16px;
    }

    .grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 1rem;
    }

    form.item-form {
        width: 100%;
        max-width: none;
        padding: 1rem;
        border-radius: 10px;
    }

    .field input[type="text"],
    .field select,
    .field textarea {
        width: 100%;
        min-height: 44px;
        font-size: 16px;
    }

    .photo-actions {
        display: grid;
        grid-template-columns: 1fr;
    }

    .photo-actions .photo-button,
    .form-actions .btn {
        width: 100%;
        min-height: 44px;
        padding: 0.75rem 1rem;
        text-align: center;
    }

    .form-actions {
        display: grid;
        grid-template-columns: 1fr;
    }
}

/* Android desktop-viewport fallback: compensate for the browser shrinking a
   980px layout onto a phone screen. */
@media (hover: none) and (pointer: coarse) and (min-width: 700px) {
    html {
        font-size: 28px;
    }

    .app-header {
        padding: 1rem;
        gap: 0.8rem;
    }

    .app-header h1 {
        font-size: 1.2rem;
    }

    .app-header > .btn {
        min-height: 72px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 0.6rem 0.9rem;
        font-size: 0.9rem;
    }

    .header-search input {
        height: 72px;
        padding-left: 3rem;
        padding-right: 7rem;
        font-size: 1rem;
    }

    .header-search button {
        top: 6px;
        right: 6px;
        height: 60px;
        padding: 0 1.2rem;
        font-size: 0.9rem;
    }

    .search-icon {
        top: 22px;
        left: 24px;
        width: 22px;
        height: 22px;
        border-width: 3px;
    }

    main {
        padding: 1rem;
    }

    .filters {
        grid-template-columns: 1fr;
        gap: 0.7rem;
        margin-bottom: 1rem;
    }

    .filters select,
    .filters a,
    .field input[type="text"],
    .field select,
    .field textarea {
        min-height: 72px;
        padding: 0.65rem 0.8rem;
        font-size: 1rem;
        border-radius: 12px;
    }

    .grid {
        grid-template-columns: 1fr;
        gap: 1rem;
    }

    .card {
        border-radius: 18px;
    }

    .card-body {
        padding: 1rem;
    }

    .card-body h3 {
        font-size: 1.15rem;
    }

    .badge,
    .card-meta,
    .results-summary,
    .photo-hint,
    .photo-preview span {
        font-size: 0.85rem;
    }

    .menu-toggle {
        width: 72px;
        height: 72px;
        font-size: 1.5rem;
    }

    form.item-form {
        padding: 1rem;
        gap: 1rem;
        border-radius: 18px;
    }

    .field {
        gap: 0.45rem;
    }

    .field label {
        font-size: 0.95rem;
    }

    .field textarea {
        min-height: 150px;
    }

    .photo-actions .photo-button,
    .form-actions .btn {
        min-height: 72px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1rem;
        border-radius: 12px;
    }
}

/* iOS Safari zooms in on focus if a form control's font-size is under 16px */
@media (max-width: 640px) {
    .field input[type="text"],
    .field select,
    .field textarea,
    .filters select {
        font-size: 16px;
    }
}
