@layer reset, base, layout, components, pages, utilities;

@layer components {
  /* Buttons ------------------------------------------------------------ */

  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 44px;
    padding: 0 18px;
    border-radius: var(--radius);
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--ink);
    font: 600 15px var(--sans);
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
  }
  .btn:hover { background: var(--press); color: var(--ink); }
  .btn:disabled, .btn[aria-disabled="true"] { opacity: .5; cursor: default; }

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

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

  .btn--lg { min-height: 50px; padding: 0 22px; font-size: 16px; }
  .btn--sm { min-height: 36px; padding: 0 12px; font-size: 14px; }
  .btn--tall { min-height: 54px; border-radius: 14px; }
  .btn--icon { padding: 0 14px; }
  .btn--block { width: 100%; }
  .btn--danger { color: var(--accent); }

  .link-button {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 0;
    border: 0;
    background: none;
    color: var(--accent);
    font: 600 14px var(--sans);
    text-decoration: none;
    cursor: pointer;
  }
  .link-button:hover { color: var(--accent-ink); text-decoration: underline; }

  .button_to { display: inline-flex; margin: 0; }

  /* Chips -------------------------------------------------------------- */

  .chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 30px;
    padding: 0 12px;
    border-radius: 999px;
    border: 0;
    background: var(--sunken);
    color: var(--ink-2);
    font: 500 13px var(--sans);
    white-space: nowrap;
    text-decoration: none;
  }
  a.chip:hover, button.chip:hover { background: var(--hover); color: var(--ink); }
  .chip--outline { background: var(--surface); border: 1px solid var(--border-strong); }
  .chip--dashed { background: transparent; border: 1px dashed var(--dashed); color: var(--muted); cursor: pointer; }
  .chip--done { background: var(--green-soft); color: var(--green-ink); }
  .chip--warn { background: var(--warn-soft); color: var(--amber-ink); }
  .chip--amber { background: var(--amber-soft); color: var(--amber-ink); }
  .chip--rose { background: var(--accent-soft); color: var(--accent-ink); }
  .chip__remove {
    width: 22px;
    height: 22px;
    margin-right: -6px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: inherit;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .chip__remove:hover { background: rgba(35, 30, 25, .08); }

  /* Toggle chips that wrap a checkbox (tags, collections) */
  .check-chip { position: relative; }
  .check-chip input { position: absolute; opacity: 0; pointer-events: none; }
  .check-chip span {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 30px;
    padding: 0 12px;
    border-radius: 999px;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
  }
  .check-chip input:checked + span { background: var(--ink); border-color: var(--ink); color: var(--surface); }
  .check-chip input:focus-visible + span { outline: 2px solid var(--ink); outline-offset: 2px; }

  .filter-chips { display: flex; gap: 8px; flex-wrap: wrap; }
  .filter-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 36px;
    padding: 0 14px;
    border-radius: 999px;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--ink-2);
    font: 500 14px var(--sans);
    text-decoration: none;
  }
  .filter-chip:hover { border-color: var(--ink); color: var(--ink); }
  .filter-chip[aria-current="true"] { background: var(--ink); border-color: var(--ink); color: var(--surface); }
  .filter-chip__count { opacity: .7; }

  /* Segmented control -------------------------------------------------- */

  .segmented { display: inline-flex; padding: 3px; background: var(--sunken); border-radius: var(--radius-sm); }
  .segmented a, .segmented button {
    display: inline-flex;
    align-items: center;
    height: 30px;
    padding: 0 12px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--ink-2);
    font: 600 13px var(--sans);
    text-decoration: none;
  }
  .segmented [aria-current="true"], .segmented [aria-pressed="true"] { background: var(--ink); color: var(--surface); }

  .segmented--soft { border-radius: var(--radius); }
  .segmented--soft a, .segmented--soft button { height: 38px; padding: 0 14px; border-radius: var(--radius-sm); font-size: 14px; color: var(--ink-3); }
  .segmented--soft [aria-current="true"], .segmented--soft [aria-pressed="true"] {
    background: var(--surface);
    color: var(--ink);
    box-shadow: 0 1px 2px rgba(35, 30, 25, .12);
  }

  .tabs { align-self: flex-start; display: inline-flex; gap: 4px; padding: 4px; background: var(--sunken); border-radius: 14px; flex-wrap: wrap; }
  .tabs a {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: 42px;
    padding: 0 16px;
    border-radius: var(--radius-sm);
    color: var(--ink-2);
    font-size: 15px;
    font-weight: 600;
    text-decoration: none;
  }
  .tabs a:hover { background: var(--hover); color: var(--ink); }
  .tabs a[aria-current="page"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(35, 30, 25, .1); }

  /* Panels ------------------------------------------------------------- */

  .panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 18px;
    min-width: 0;
  }
  .panel--tight { padding: 18px 20px; gap: 12px; border-radius: 18px; }
  .panel--muted { background: var(--sunken); border-color: var(--sunken); }
  .panel--dark { background: var(--ink); border-color: var(--ink); color: var(--surface); }
  .panel--dark p, .panel--dark .muted { color: #D8CEC1; }
  .panel__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
  .panel__title { font-size: 17px; font-weight: 600; }
  .panel__title--serif { font: 500 28px var(--serif); }
  .panel__foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
    padding-top: 16px;
    border-top: 1px solid var(--line);
  }

  .section-label {
    font-size: 12px;
    font-weight: 600;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--muted);
  }

  /* Forms -------------------------------------------------------------- */

  .field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
  .field > label, .field__label { font-size: 13px; font-weight: 600; color: var(--ink-3); }
  .field__hint { font-size: 13px; color: var(--muted); }

  .input, .textarea, .select {
    width: 100%;
    min-height: 46px;
    padding: 0 14px;
    border-radius: var(--radius);
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--ink);
    font: 400 16px var(--sans);
  }
  .textarea { padding: 12px 14px; min-height: 76px; resize: vertical; line-height: 1.5; }
  .textarea--sunken, .input--sunken { background: var(--bg); }
  .textarea--tall { min-height: 220px; }
  .input--lg { min-height: 54px; }
  .input--serif { font: 400 19px var(--serif); }
  .select { width: auto; min-height: 44px; padding: 0 12px; font-weight: 500; font-size: 15px; }

  .field-grid { display: grid; gap: 12px; grid-template-columns: repeat(var(--cols, 2), minmax(0, 1fr)); }

  .form-errors {
    padding: 14px 16px;
    border-radius: var(--radius);
    background: var(--del-soft);
    color: var(--del-ink);
    font-size: 15px;
  }
  .form-errors ul { margin: 6px 0 0; padding-left: 20px; }

  .checkbox {
    width: 22px;
    height: 22px;
    margin: 0;
    flex-shrink: 0;
    accent-color: var(--green);
    cursor: pointer;
  }

  /* Search ------------------------------------------------------------- */

  .search-row { display: flex; gap: 12px; }
  .search-box {
    flex-grow: 1;
    display: flex;
    align-items: center;
    gap: 12px;
    height: 54px;
    padding: 0 18px;
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: 14px;
    color: var(--muted);
  }
  /* Straight in the page's column, growing would stretch it down the page. */
  .main > .search-box { flex: none; }
  .search-box:focus-within { border-color: var(--ink); }
  .search-box input {
    flex-grow: 1;
    min-width: 0;
    border: 0;
    outline: none;
    background: transparent;
    color: var(--ink);
    font-size: 16px;
  }
  .kbd { font-size: 12px; border: 1px solid var(--border-strong); border-radius: 6px; padding: 2px 7px; }

  /* Flash -------------------------------------------------------------- */

  .flash {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-radius: 14px;
    background: var(--green-soft);
    color: var(--green-ink);
    font-size: 15px;
    font-weight: 500;
  }
  .flash--alert { background: var(--del-soft); color: var(--del-ink); }

  /* Status checks ------------------------------------------------------ */

  .checks { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 14px; font-size: 15px; line-height: 1.4; }
  .checks li { display: flex; gap: 12px; }
  .check-dot {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--green);
    color: var(--surface);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }
  .check-dot .icon { width: 14px; height: 14px; stroke-width: 2.5; }
  .check-dot--pending { background: var(--sunken); color: var(--muted); }
  .check-dot--pending .icon { animation: spin 1s linear infinite; }
  .check-dot--failed { background: var(--accent); }

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

  /* Recipe cards ------------------------------------------------------- */

  .recipe-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 24px; }
  .recipe-card {
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    color: var(--ink);
    text-decoration: none;
    transition: box-shadow .15s, transform .15s;
  }
  .recipe-card:hover { color: var(--ink); box-shadow: var(--shadow); transform: translateY(-2px); }
  .recipe-card__media { height: 164px; position: relative; }
  .recipe-card__body { padding: 14px 16px 16px; display: flex; flex-direction: column; gap: 8px; flex-grow: 1; }
  .recipe-card__title { font: 500 19px/1.2 var(--serif); }
  .recipe-card__meta { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
  .recipe-card__source {
    margin-top: auto;
    display: flex;
    align-items: center;
    gap: 6px;
    padding-top: 8px;
    border-top: 1px solid var(--line);
    font-size: 13px;
    color: var(--muted);
    min-width: 0;
  }
  .badge-new {
    position: absolute;
    top: 12px;
    left: 12px;
    height: 24px;
    padding: 0 10px;
    border-radius: 999px;
    background: var(--ink);
    color: var(--surface);
    font-size: 12px;
    font-weight: 600;
    display: flex;
    align-items: center;
  }
  .badge-fav {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--surface);
    color: var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Photo or placeholder plate */
  .media {
    width: 100%;
    height: 100%;
    background: var(--plate, #D9A873);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    border-radius: inherit;
  }
  .media img { width: 100%; height: 100%; object-fit: cover; }
  .media__plate {
    width: 56%;
    max-width: 200px;
    aspect-ratio: 1;
    border-radius: 50%;
    border: 2px solid rgba(255, 253, 248, .6);
    box-shadow: inset 0 0 0 14px rgba(255, 253, 248, .2);
  }

  .thumb { width: 44px; height: 44px; border-radius: var(--radius-sm); flex-shrink: 0; overflow: hidden; }

  /* Notes -------------------------------------------------------------- */

  .note {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 14px 16px;
    border-radius: 14px;
    background: var(--amber-soft);
  }
  .note--plain { background: var(--bg); }
  .note--selected { outline: 2px solid var(--ink); outline-offset: 2px; }
  .note__date { font-size: 13px; font-weight: 600; color: var(--amber-ink); }
  .note--plain .note__date { color: var(--muted); }
  .note__body { font-size: 16px; line-height: 1.5; white-space: pre-line; }
  .note__actions { display: flex; gap: 12px; align-items: center; }

  .empty-state {
    padding: 40px;
    text-align: center;
    border: 1px dashed var(--dashed);
    border-radius: 18px;
    color: var(--ink-3);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
  }

  @media (max-width: 1200px) {
    .recipe-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  }
  @media (max-width: 900px) {
    .recipe-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
    .recipe-card__media { height: 124px; }
    .recipe-card__title { font-size: 17px; }
    .search-row { flex-direction: column; }
    .search-box .kbd { display: none; }
    .panel { padding: 18px; }
    .field-grid { grid-template-columns: minmax(0, 1fr); }
  }
}

/*
 * A stored photo that's slow to come or didn't load (photo_controller.js): a
 * spinner where it goes, then a camera once it's stopped trying. In utilities,
 * so a page's own background on its photos (a cookbook cover's) can't hide them.
 */
@layer utilities {
  img.photo--pending, img.photo--missing { background-position: center; background-repeat: no-repeat; background-size: 32px; }
  img.photo--pending {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3e%3ccircle cx='16' cy='16' r='16' fill='%23FFFDF8' fill-opacity='.85'/%3e%3cpath d='M16 8.5a7.5 7.5 0 1 0 7.5 7.5' fill='none' stroke='%236A5F55' stroke-width='2' stroke-linecap='round'%3e%3canimateTransform attributeName='transform' type='rotate' from='0 16 16' to='360 16 16' dur='1s' repeatCount='indefinite'/%3e%3c/path%3e%3c/svg%3e");
  }
  img.photo--missing {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3e%3ccircle cx='16' cy='16' r='16' fill='%23FFFDF8' fill-opacity='.85'/%3e%3cg transform='translate(6 6) scale(.8333)' fill='none' stroke='%236A5F55' stroke-width='2.1' stroke-linecap='round' stroke-linejoin='round'%3e%3cpath d='M4 8h3l2-3h6l2 3h3v11H4z'/%3e%3ccircle cx='12' cy='13' r='3.5'/%3e%3c/g%3e%3c/svg%3e");
  }
}
