/* Reset & base */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --blue: #007AFF;
    --green: #34C759;
    --red: #FF3B30;
    --gray: #8E8E93;
    --light-gray: #F2F2F7;
    --text-primary: #1C1C1E;
    --text-secondary: #8E8E93;
    --background: #F2F2F7;
    --card-bg: #FFFFFF;
    --border: #E5E5EA;
    --shadow: rgba(0, 0, 0, 0.08);
}

@media (prefers-color-scheme: dark) {
    :root {
        --text-primary: #FFFFFF;
        --text-secondary: #98989D;
        --background: #000000;
        --card-bg: #1C1C1E;
        --border: #38383A;
        --shadow: rgba(0, 0, 0, 0.3);
        --light-gray: #2C2C2E;
    }
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--background);
    color: var(--text-primary);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    min-height: 100vh;
}

#app {
    max-width: 600px;
    margin: 0 auto;
    padding: 0 16px;
}

/* States */
.state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 60vh;
    text-align: center;
    padding: 20px;
}

.state-icon {
    font-size: 48px;
    margin-bottom: 16px;
}

.state h2 {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 8px;
}

.state p {
    color: var(--text-secondary);
    font-size: 15px;
    margin-bottom: 20px;
}

.hidden {
    display: none !important;
}

/* Spinner */
.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--blue);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-bottom: 16px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Header */
header {
    text-align: center;
    padding: 24px 0 16px;
}

header h1 {
    font-size: 24px;
    font-weight: 700;
}

header .subtitle {
    color: var(--text-secondary);
    font-size: 13px;
    margin-top: 4px;
}

/* Search bar */
.search-bar {
    margin-bottom: 12px;
}

#search-input {
    width: 100%;
    padding: 10px 16px;
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 15px;
    background: var(--card-bg);
    color: var(--text-primary);
    outline: none;
    -webkit-appearance: none;
}

#search-input:focus {
    border-color: var(--blue);
}

#search-input::placeholder {
    color: var(--text-secondary);
}

/* Tabs row */
.tabs-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.tabs {
    flex: 1;
    display: flex;
    background: var(--light-gray);
    border-radius: 10px;
    padding: 2px;
}

.search-toggle-btn {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    padding: 6px;
    border-radius: 8px;
    flex-shrink: 0;
    line-height: 1;
    transition: background 0.15s;
}

.search-toggle-btn:active {
    background: var(--light-gray);
}

.tab {
    flex: 1;
    padding: 8px 16px;
    border: none;
    background: transparent;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.tab.active {
    background: var(--card-bg);
    color: var(--text-primary);
    box-shadow: 0 1px 3px var(--shadow);
}

/* All / Pending filter */
.filter-segment {
    display: inline-flex;
    background: var(--light-gray);
    border-radius: 8px;
    padding: 2px;
    margin-bottom: 16px;
}

.filter-btn {
    padding: 5px 18px;
    border: none;
    background: transparent;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s;
}

.filter-btn.active {
    background: var(--card-bg);
    color: var(--text-primary);
    box-shadow: 0 1px 3px var(--shadow);
}

/* Categories */
.category {
    background: var(--card-bg);
    border-radius: 12px;
    margin-bottom: 12px;
    overflow: hidden;
    box-shadow: 0 1px 3px var(--shadow);
}

.category-header {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    font-weight: 600;
    font-size: 15px;
    cursor: pointer;
    user-select: none;
    gap: 8px;
}

.category-header .emoji {
    font-size: 18px;
}

.category-header .name {
    flex: 1;
}

.category-header .count {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 400;
}

.category-header .chevron {
    color: var(--text-secondary);
    width: 14px;
    height: 14px;
    flex-shrink: 0;
    transition: transform 0.2s;
    transform: rotate(90deg);
}

.category.collapsed .chevron {
    transform: rotate(0deg);
}

.category.collapsed .category-items {
    display: none;
}

/* Items */
.category-items {
    border-top: 1px solid var(--border);
}

.item {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    gap: 12px;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    transition: background 0.15s;
}

.item:last-child {
    border-bottom: none;
}

.item:active {
    background: var(--light-gray);
}

.item .checkbox {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 2px solid var(--border);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.item.checked .checkbox {
    background: var(--green);
    border-color: var(--green);
}

.item.checked .checkbox::after {
    content: '\2713';
    color: white;
    font-size: 13px;
    font-weight: 700;
}

.item .item-info {
    flex: 1;
    min-width: 0;
}

.item .item-title {
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.item.checked .item-title {
    text-decoration: line-through;
    color: var(--text-secondary);
}

.item .item-notes {
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.item .quantity {
    font-size: 13px;
    color: var(--text-secondary);
    background: var(--light-gray);
    padding: 2px 8px;
    border-radius: 10px;
    flex-shrink: 0;
}

/* Progress */
.progress-bar {
    height: 4px;
    background: var(--light-gray);
    border-radius: 2px;
    margin: 0 16px 8px;
    overflow: hidden;
}

.progress-bar .fill {
    height: 100%;
    background: var(--green);
    border-radius: 2px;
    transition: width 0.3s ease;
}

/* Empty category */
.empty-category {
    padding: 16px;
    text-align: center;
    color: var(--text-secondary);
    font-size: 14px;
    font-style: italic;
}

/* Guest identity */
#guest-identity {
    margin-bottom: 12px;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 14px 16px;
}

.guest-identity-title {
    display: block;
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 8px;
}

#guest-name {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 15px;
    background: var(--light-gray);
    color: var(--text-primary);
    outline: none;
    -webkit-appearance: none;
    transition: border-color 0.15s;
}

#guest-name:focus {
    border-color: var(--blue);
}

.suggest-btn {
    width: 100%;
    padding: 14px;
    border: none;
    border-radius: 12px;
    background: var(--blue);
    color: white;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s;
}

.suggest-btn:active {
    opacity: 0.8;
}

/* Toast */
.toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 10px 20px;
    border-radius: 20px;
    font-size: 14px;
    z-index: 1000;
    animation: fadeInUp 0.3s ease, fadeOut 0.3s ease 2s forwards;
}

@keyframes fadeInUp {
    from { opacity: 0; transform: translateX(-50%) translateY(10px); }
    to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

@keyframes fadeOut {
    from { opacity: 1; }
    to { opacity: 0; }
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    z-index: 100;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background: var(--card-bg);
    border-radius: 16px 16px 0 0;
    padding: 24px 20px;
    width: 100%;
    max-width: 600px;
    max-height: 85vh;
    overflow-y: auto;
    animation: slideUp 0.3s ease;
}

@keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

.modal-content h3 {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 16px;
    text-align: center;
}

.modal-content select,
.modal-content > input[type="text"],
.modal-content > input[type="number"],
.modal-content > input[type="password"] {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 15px;
    background: var(--card-bg);
    color: var(--text-primary);
    margin-bottom: 12px;
    -webkit-appearance: none;
}

.modal-actions {
    display: flex;
    gap: 12px;
    margin-top: 8px;
}

.btn-cancel,
.btn-primary {
    flex: 1;
    padding: 12px;
    border: none;
    border-radius: 10px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
}

.btn-cancel {
    background: var(--light-gray);
    color: var(--text-primary);
}

.btn-primary {
    background: var(--blue);
    color: white;
}

/* App link */
.app-link {
    display: inline-block;
    padding: 12px 24px;
    background: var(--blue);
    color: white;
    text-decoration: none;
    border-radius: 12px;
    font-weight: 600;
    font-size: 15px;
}

/* Footer */
footer {
    text-align: center;
    padding: 20px 0 40px;
    color: var(--text-secondary);
    font-size: 13px;
}

footer a {
    color: var(--blue);
    text-decoration: none;
}

/* Pending suggestion badges */
.pending-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 4px;
}

.pending-badge {
    font-size: 11px;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 8px;
    display: inline-block;
}

.pending-badge-check {
    background: #E8F5E9;
    color: #2E7D32;
}

.pending-badge-delete {
    background: #FFEBEE;
    color: #C62828;
}

.pending-badge-edit {
    background: #E3F2FD;
    color: #1565C0;
}

.pending-badge-new {
    background: #FFF3E0;
    color: #E65100;
}

@media (prefers-color-scheme: dark) {
    .pending-badge-check { background: #1B5E20; color: #A5D6A7; }
    .pending-badge-delete { background: #B71C1C; color: #EF9A9A; }
    .pending-badge-edit { background: #0D47A1; color: #90CAF9; }
    .pending-badge-new { background: #BF360C; color: #FFCC80; }
}

/* Pending item visual states */
.item.has-pending {
    background: var(--light-gray);
}

.item.pending-delete {
    opacity: 0.5;
}

.item.pending-delete .item-title {
    text-decoration: line-through;
    color: var(--red);
}

.item.pending-check .checkbox {
    border-color: var(--green);
    border-style: dashed;
}

.item.pending-new-item {
    border-left: 3px solid #FF9800;
    opacity: 0.7;
}

/* Item delete button (x) */
.item-delete-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 22px;
    font-weight: 300;
    padding: 8px 10px;
    cursor: pointer;
    flex-shrink: 0;
    border-radius: 6px;
    line-height: 1;
    transition: color 0.15s;
    min-width: 40px;
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.item-delete-btn:hover {
    color: var(--red);
}

.item-delete-btn:active {
    background: var(--light-gray);
    color: var(--red);
}

.item-delete-btn.already-suggested {
    color: var(--red);
    opacity: 0.6;
}

/* Item edit button (pencil) */
.item-edit-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 18px;
    padding: 8px;
    cursor: pointer;
    flex-shrink: 0;
    border-radius: 6px;
    line-height: 1;
    transition: color 0.15s;
    min-width: 36px;
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.item-edit-btn:hover {
    color: var(--blue);
}

.item-edit-btn:active {
    background: var(--light-gray);
    color: var(--blue);
}

/* Edit item modal: quantity (fixed) then name (fills the row) */
.edit-item-fields {
    display: flex;
    gap: 12px;
    margin-bottom: 12px;
}

.edit-item-fields input {
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 15px;
    background: var(--card-bg);
    color: var(--text-primary);
    -webkit-appearance: none;
}

.edit-item-fields .edit-item-qty {
    width: 80px;
    flex-shrink: 0;
    text-align: center;
    -moz-appearance: textfield;
}

.edit-item-fields .edit-item-name {
    flex: 1;
    min-width: 0;
}

/* Category add button (+) */
.category-add-btn {
    background: none;
    border: 1px solid var(--border);
    color: var(--blue);
    font-size: 18px;
    font-weight: 500;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.15s;
    line-height: 1;
    padding: 0;
}

.category-add-btn:hover {
    background: var(--blue);
    color: white;
    border-color: var(--blue);
}

.category-add-btn:active {
    transform: scale(0.9);
}

/* Inline add row */
.inline-add {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    gap: 8px;
    border-bottom: 1px solid var(--border);
    background: var(--light-gray);
}

.inline-add-input {
    flex: 1;
    min-width: 0;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 15px;
    background: var(--card-bg);
    color: var(--text-primary);
    outline: none;
}

.inline-add-input:focus {
    border-color: var(--blue);
}

.inline-add-qty {
    width: 56px;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 15px;
    background: var(--card-bg);
    color: var(--text-primary);
    text-align: center;
    outline: none;
    -webkit-appearance: none;
    -moz-appearance: textfield;
}

.inline-add-qty:focus {
    border-color: var(--blue);
}

.inline-add-submit {
    background: var(--blue);
    color: white;
    border: none;
    border-radius: 8px;
    padding: 10px 16px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    flex-shrink: 0;
    min-height: 40px;
}

.inline-add-submit:active {
    opacity: 0.8;
}

.inline-add-cancel {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 22px;
    cursor: pointer;
    min-width: 40px;
    min-height: 40px;
    flex-shrink: 0;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Inline add checkbox */
.inline-add-checkbox {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 2px solid var(--border);
    flex-shrink: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.inline-add-checkbox.checked {
    background: var(--green);
    border-color: var(--green);
}

.inline-add-checkbox.checked::after {
    content: '\2713';
    color: white;
    font-size: 13px;
    font-weight: 700;
}

/* Add category button */
.add-category-btn {
    width: 100%;
    padding: 14px;
    border: 2px dashed var(--border);
    border-radius: 12px;
    background: transparent;
    color: var(--blue);
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    margin-bottom: 12px;
    transition: all 0.15s;
}

.add-category-btn:active {
    background: var(--light-gray);
}

/* Pending new category */
.pending-new-category {
    border-left: 3px solid #FF9800;
    opacity: 0.8;
}

/* New category modal items */
.new-cat-items-header {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 8px;
    margin-top: 4px;
}

.new-cat-items-wrapper {
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 16px;
}

.new-cat-items-list {
    /* items render here, wrapper handles border */
}

.new-cat-item-row {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    gap: 8px;
    border-bottom: 1px solid var(--border);
    font-size: 15px;
}

.new-cat-item-row:last-child {
    border-bottom: none;
}

.new-cat-item-row.checked .new-cat-item-title {
    text-decoration: line-through;
    color: var(--text-secondary);
}

.new-cat-item-check {
    width: 20px;
    color: var(--green);
    font-size: 13px;
    font-weight: 700;
    flex-shrink: 0;
}

.new-cat-item-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.new-cat-item-remove {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 18px;
    cursor: pointer;
    padding: 2px 6px;
    line-height: 1;
    flex-shrink: 0;
}

.new-cat-item-remove:hover {
    color: var(--red);
}

.new-cat-add-row {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    gap: 8px;
    background: var(--light-gray);
    border-top: 1px solid var(--border);
}

.new-cat-items-list:empty + .new-cat-add-row {
    border-top: none;
}

.new-cat-add-row .inline-add-qty {
    flex-shrink: 0;
}

.new-cat-add-row .inline-add-submit {
    flex-shrink: 0;
}

/* Safe area for iOS */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    footer {
        padding-bottom: calc(40px + env(safe-area-inset-bottom));
    }
}
