:root {
    --bg: #f4f1ea;
    --ink: #23291f;
    --muted: #7c8173;
    --line: #e5e1d6;
    --accent: #3f6b4a;
    --accent-dark: #345a3d;
    --accent-soft: #e6efe6;
    --danger: #a3402f;
    --card: #ffffff;
    --shadow: 0 1px 2px rgba(35, 41, 31, 0.04), 0 6px 18px rgba(35, 41, 31, 0.06);
    --radius: 14px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

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

.wrap {
    max-width: 540px;
    margin: 0 auto;
    padding: max(24px, env(safe-area-inset-top)) 16px 64px;
}

button { font-family: inherit; cursor: pointer; }

/* --- Header --- */
.top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
}

h1 {
    font-size: 22px;
    font-weight: 650;
    margin: 0;
    letter-spacing: -0.02em;
}

.count-pill {
    flex: 0 0 auto;
    min-width: 44px;
    padding: 5px 12px;
    border-radius: 999px;
    background: var(--card);
    border: 1px solid var(--line);
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
    text-align: center;
    font-variant-numeric: tabular-nums;
    transition: background 0.2s, color 0.2s, border-color 0.2s;
}

.count-pill.complete {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

/* --- Progress bar --- */
.progress-track {
    height: 6px;
    border-radius: 999px;
    background: var(--line);
    overflow: hidden;
    margin-bottom: 20px;
}

.progress-fill {
    height: 100%;
    width: 0;
    border-radius: 999px;
    background: var(--accent);
    transition: width 0.35s ease;
}

/* --- Card base --- */
.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

/* --- Add card --- */
.add-card {
    padding: 14px;
    margin-bottom: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.field {
    width: 100%;
    border: 1.5px solid var(--line);
    border-radius: 10px;
    background: #fcfbf8;
    color: var(--ink);
    font-size: 16px;
    padding: 13px 14px;
    transition: border-color 0.15s, background 0.15s;
}

.field::placeholder { color: #a9ad9f; }

.field:focus {
    outline: none;
    border-color: var(--accent);
    background: #fff;
}

.field-main {
    flex: 1;
    min-width: 0;
    font-size: 17px;
    font-weight: 500;
}

.add-card-row {
    display: flex;
    gap: 10px;
    align-items: stretch;
}

/* --- Quantity stepper --- */
.stepper {
    flex: 0 0 auto;
    display: flex;
    align-items: stretch;
    border: 1.5px solid var(--line);
    border-radius: 10px;
    background: #fcfbf8;
    overflow: hidden;
}

.step {
    width: 40px;
    border: none;
    background: none;
    color: var(--accent);
    font-size: 22px;
    font-weight: 600;
    line-height: 1;
    padding: 0;
    transition: background 0.12s;
}

.step:active { background: var(--accent-soft); }
.step:disabled { color: #c7cabb; }

.qty {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 28px;
    font-size: 16px;
    font-weight: 650;
    color: var(--ink);
    font-variant-numeric: tabular-nums;
    border-left: 1.5px solid var(--line);
    border-right: 1.5px solid var(--line);
}

.primary {
    width: 100%;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 10px;
    padding: 13px 20px;
    font-size: 15px;
    font-weight: 600;
    letter-spacing: 0.01em;
    transition: background 0.15s, transform 0.05s;
}

.primary:hover { background: var(--accent-dark); }
.primary:active { transform: scale(0.99); }
.primary:disabled { opacity: 0.5; cursor: not-allowed; }

/* --- Frequent chips --- */
.frequent-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 20px;
}

.frequent-row.hidden { display: none; }

.chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 8px 14px 8px 11px;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 999px;
    font-size: 13.5px;
    font-weight: 500;
    color: var(--ink);
    box-shadow: var(--shadow);
    transition: border-color 0.15s, background 0.15s, transform 0.05s;
}

.chip:hover { border-color: var(--accent); background: var(--accent-soft); }
.chip:active { transform: scale(0.96); }

.chip-plus {
    color: var(--accent);
    font-weight: 700;
    font-size: 15px;
    line-height: 1;
}

/* --- List --- */
.list {
    list-style: none;
    margin: 0;
    padding: 4px;
    overflow: hidden;
}

.list.hidden { display: none; }

.item {
    display: flex;
    align-items: center;
    gap: 13px;
    padding: 13px 10px;
    border-radius: 10px;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.12s;
}

.item + .item { border-top: 1px solid var(--line); }
.item:active { background: #f6f4ef; }
@media (hover: hover) {
    .item:hover { background: #f9f8f4; }
}

.check {
    flex: 0 0 auto;
    width: 24px;
    height: 24px;
    border: 2px solid #cdd0c3;
    border-radius: 50%;
    background: #fff;
    position: relative;
    transition: background 0.15s, border-color 0.15s;
}

.item.done .check {
    background: var(--accent);
    border-color: var(--accent);
}

.check::after {
    content: "";
    position: absolute;
    left: 7px;
    top: 3px;
    width: 6px;
    height: 11px;
    border: solid #fff;
    border-width: 0 2.5px 2.5px 0;
    transform: rotate(45deg) scale(0);
    transition: transform 0.15s ease;
}

.item.done .check::after { transform: rotate(45deg) scale(1); }

.item-text {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.item-name {
    font-size: 15.5px;
    line-height: 1.3;
    word-break: break-word;
    transition: color 0.15s;
}

.item-qty {
    display: inline-block;
    margin-left: 7px;
    padding: 1px 7px;
    border-radius: 6px;
    background: var(--accent-soft);
    color: var(--accent);
    font-size: 12px;
    font-weight: 650;
    font-variant-numeric: tabular-nums;
    vertical-align: 1px;
    white-space: nowrap;
}

.item.done .item-qty {
    background: var(--line);
    color: var(--muted);
}

.item-note {
    font-size: 12.5px;
    color: var(--muted);
}

.item.done .item-name {
    text-decoration: line-through;
    text-decoration-color: rgba(124, 129, 115, 0.6);
    color: var(--muted);
}

.del-btn {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    background: none;
    color: #bfc3b4;
    border-radius: 8px;
    transition: color 0.15s, background 0.15s;
}

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

/* --- Empty state --- */
.empty {
    text-align: center;
    color: var(--muted);
    font-size: 14.5px;
    padding: 44px 20px;
}

.empty.hidden { display: none; }
.empty p { margin: 0; line-height: 1.6; }

.empty-icon {
    font-size: 40px;
    margin-bottom: 12px;
    opacity: 0.85;
}

/* --- Actions --- */
.actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px 20px;
    margin-top: 20px;
}

.actions.hidden { display: none; }

.link-btn {
    border: none;
    background: none;
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
    text-decoration: underline;
    text-underline-offset: 3px;
    text-decoration-color: var(--line);
    padding: 6px 2px;
    transition: color 0.15s;
}

.link-btn:hover { color: var(--accent); }
.link-btn.danger:hover { color: var(--danger); }
.link-btn.hidden { display: none; }

@media (max-width: 360px) {
    .field-main { font-size: 16px; padding: 12px; }
    .step { width: 36px; }
}

/* --- Templates modal --- */
body.modal-open { overflow: hidden; }

.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    background: rgba(35, 41, 31, 0.45);
    padding: 0;
}

.modal-overlay.hidden { display: none; }

.modal {
    width: 100%;
    max-width: 540px;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -8px 40px rgba(35, 41, 31, 0.25);
    animation: sheet-up 0.22s ease;
}

@keyframes sheet-up {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 16px 12px;
    border-bottom: 1px solid var(--line);
}

.modal-head h2 {
    margin: 0;
    font-size: 17px;
    font-weight: 650;
    letter-spacing: -0.01em;
}

.modal-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border: none;
    background: none;
    color: var(--muted);
    border-radius: 8px;
}

.modal-close:hover { background: var(--line); color: var(--ink); }

.modal-body {
    padding: 14px 16px 20px;
    overflow-y: auto;
}

.tpl-list {
    list-style: none;
    margin: 0 0 14px;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.tpl-list:empty { margin: 0; }

.tpl-item {
    display: flex;
    gap: 8px;
    align-items: stretch;
}

.tpl-load {
    flex: 1;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
    padding: 13px 15px;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 11px;
    box-shadow: var(--shadow);
    text-align: left;
    transition: border-color 0.15s, background 0.12s, transform 0.05s;
}

.tpl-load:hover { border-color: var(--accent); background: var(--accent-soft); }
.tpl-load:active { transform: scale(0.99); }

.tpl-name { font-size: 15px; font-weight: 600; color: var(--ink); }
.tpl-count { font-size: 12.5px; color: var(--muted); white-space: nowrap; }

.tpl-del {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    border: 1px solid var(--line);
    background: var(--card);
    border-radius: 11px;
    color: #bfc3b4;
    transition: color 0.15s, border-color 0.15s;
}

.tpl-del:hover { color: var(--danger); border-color: var(--danger); }

.tpl-empty {
    margin: 4px 0 14px;
    padding: 20px;
    text-align: center;
    font-size: 13.5px;
    color: var(--muted);
}

.tpl-empty.hidden { display: none; }

.tpl-save {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding-top: 14px;
    border-top: 1px solid var(--line);
}

.tpl-save input {
    width: 100%;
    border: 1.5px solid var(--line);
    border-radius: 10px;
    background: var(--card);
    color: var(--ink);
    font-size: 16px;
    padding: 12px 14px;
}

.tpl-save input:focus { outline: none; border-color: var(--accent); }

.tpl-hint {
    margin: 12px 0 0;
    font-size: 12px;
    line-height: 1.5;
    color: var(--muted);
}
