@media (max-width: 820px) {
  html, body { width: 100%; max-width: 100%; overflow-x: hidden; }
  body { display: block; min-height: 100dvh; }
  .sidebar { position: relative; height: auto; padding: 10px 12px 8px; }
  .brand { gap: 9px; margin: 0 2px 8px; }
  .brand img { width: 38px; height: 38px; border-radius: 0; background: transparent; }
  .brand strong { font-size: 15px; }
  .brand small { font-size: 11px; }
  .sidebar nav { display: flex; gap: 4px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .sidebar nav::-webkit-scrollbar { display: none; }
  .nav-title { display: none; }
  .sidebar nav button { flex: 0 0 auto; white-space: nowrap; padding: 9px 11px; font-size: 13px; }
  .user { display: flex; flex: 0 0 auto; gap: 4px; }
  #userAvatar { width: 30px; height: 30px; border-radius: 8px; }
  #userAvatar svg { width: 19px; height: 19px; }
  .user > div { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  .user .logout { display: inline-flex; align-items: center; min-height: 44px; margin-left: 0; padding: 8px; }
  header { position: relative; top: auto; height: auto; min-height: 66px; padding: 8px 12px; gap: 8px; }
  .restaurant-switcher { width: auto; flex: 1; min-width: 0; gap: 9px; }
  .restaurant-switcher label { min-width: 0; flex: 1; }
  .restaurant-switcher select { min-width: 0; width: 100%; font-size: 16px; }
  .header-mark { display: block; width: 76px; height: 50px; flex-basis: 76px; }
  #content { width: 100%; padding: 18px 12px 30px; }
  .title { align-items: flex-start; flex-direction: column; margin-bottom: 18px; gap: 12px; }
  .title h1 { font-size: 24px; }
  .title p { font-size: 14px; }
  .title > .primary { width: 100%; }
  .panel, .card { padding: 15px; border-radius: 12px; }
  .form, .component, .restaurant-line { grid-template-columns: 1fr 1fr; }
  .cards { grid-template-columns: 1fr 1fr; gap: 10px; }
  .card { min-height: 112px; }
  .metric { font-size: 25px; }
  .table { margin-top: 14px; border-radius: 12px; -webkit-overflow-scrolling: touch; }
  table { min-width: 680px; }
  th, td { padding: 12px; font-size: 12px; }
  input, select, textarea { font-size: 16px; }
  button.primary { min-height: 44px; }
  dialog { width: calc(100vw - 16px); max-width: none; max-height: calc(100dvh - 16px); padding: 18px; border-radius: 14px; }
  .toast { left: 12px; right: 12px; bottom: max(12px, env(safe-area-inset-bottom)); text-align: center; }
  .auth-screen { padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom)); }
  .auth-card { padding: 26px 22px; border-radius: 18px; }
  .auth-card img { display: block; width: 76px; height: 76px; object-fit: contain; }
  .auth-card h1 { font-size: 27px; }
  .auth-card input, .auth-card button { min-height: 46px; }
  .dashboard-toolbar { justify-content:stretch; margin:0 0 16px; }
  .dashboard-toolbar label,.dashboard-toolbar select { width:100%; }
  .header-actions { flex: 0 0 auto; gap: 6px; }
  .theme-toggle { width: 42px; height: 42px; justify-content: center; padding: 0; }
  .theme-toggle span:last-child { display: none; }
}

@media (max-width: 520px) {
  dialog.recipe-dialog { width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; padding: 52px 12px max(18px, env(safe-area-inset-bottom)); border-radius: 0; }
  .recipe-dialog > form[method=dialog] { position: fixed; top: max(10px, env(safe-area-inset-top)); right: 12px; z-index: 3; }
  .recipe-dialog .close { background: var(--accent-soft); color: var(--accent); box-shadow: 0 2px 8px #0002; }
  .recipe-book { padding: 0; border: 0; }
  .recipe-book-brand { height: 52px; margin-bottom: 8px; }
  .recipe-book-brand img { max-width: 150px; max-height: 48px; }
  .recipe-name-bar { padding: 7px 8px; border-radius: 9px; margin-bottom: 16px; }
  .recipe-name-bar input { font-size: 18px; min-height: 38px; }
  .recipe-description { margin-bottom: 18px; }
  .recipe-description label { font-size: 11px; }
  .recipe-grid { border: 0; overflow: visible; }
  .recipe-grid-head { display: none; }
  .recipe-component { position: relative; min-width: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 0 0 12px; padding: 16px 44px 16px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-soft); }
  .recipe-component:first-child { border-top: 1px solid var(--border); }
  .component-field { min-width: 0; }
  .component-field>span { display: block; margin: 0 0 5px; color: var(--muted); font-size: 11px; font-weight: 700; }
  .component-name, .component-kind { grid-column: 1 / -1; }
  .recipe-component input, .recipe-component select { min-height: 46px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface); }
  .recipe-component .component-yield-value { min-height: 46px; justify-content: flex-start; padding: 12px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface); }
  .recipe-component .remove { position: absolute; top: 9px; right: 8px; width: 34px; height: 34px; padding: 0; border-radius: 50%; background: var(--accent-soft); font-size: 22px; line-height: 1; }
  .recipe-add { width: 100%; min-height: 44px; margin: 4px 0 16px; border: 1px dashed #8ca598; border-radius: 9px; }
  .recipe-yield { grid-template-columns: 1fr 1fr; gap: 10px; padding: 14px; border: 1px solid #d9dfdc; border-radius: 12px; }
  .recipe-yield strong { grid-column: 1 / -1; padding: 0; text-align: left; font-size: 12px; }
  .recipe-yield input, .recipe-yield select { min-height: 46px; border: 1px solid #d9dfdc; border-radius: 9px; }
  .recipe-development { margin-top: 12px; border: 1px solid #d9dfdc; border-radius: 12px; overflow: hidden; }
  .recipe-development label { padding: 12px; text-align: left; font-size: 12px; }
  .recipe-development textarea { min-height: 140px; padding: 12px; }
  .recipe-operations { grid-template-columns: 1fr; margin-top: 12px; border: 1px solid #d9dfdc; border-radius: 12px; overflow: hidden; }
  .recipe-operations label { text-align: left; }
  .recipe-operations label + label { border-left: 0; border-top: 1px solid #d9dfdc; }
  .recipe-operations strong { padding: 12px; }
  .recipe-operations textarea { min-height: 105px; padding: 12px; }
  .recipe-bottom { grid-template-columns: 1fr; }
  .recipe-bottom { margin-top: 12px; border: 1px solid #d9dfdc; border-radius: 12px; overflow: hidden; }
  .recipe-settings { padding: 14px; border-right: 0; border-bottom: 1px solid #d9dfdc; }
  .recipe-config-title { padding: 0 0 12px; text-align: left; }
  .restaurant-line { grid-template-columns: 1fr; gap: 8px; margin: 0 0 14px; padding: 12px; border: 1px solid #e1e6e3; border-radius: 10px; }
  .restaurant-line:last-child { margin-bottom: 0; }
  .recipe-photo { min-height: 170px; padding: 18px 14px; }
  .recipe-actions { position: sticky; bottom: calc(-1 * max(18px, env(safe-area-inset-bottom))); z-index: 2; margin: 16px -12px calc(-1 * max(18px, env(safe-area-inset-bottom))); padding: 12px 12px max(12px, env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--border); }
  .recipe-actions .primary { width: 100%; }
}

@media (max-width: 520px) {
  .cards { grid-template-columns: 1fr; }
  .form { grid-template-columns: 1fr; }
  .header-mark { width: 64px; height: 44px; flex-basis: 64px; }
  .photo-input { width: 100%; }
}

/* Recetas y pre-elaborados: una sola aplicación, presentación adaptable. */
.entity-list-table .photo-upload-action{
  position:relative;display:inline-flex;align-items:center;justify-content:center;
  min-height:36px;margin-top:8px;padding:7px 11px;border:1px solid var(--border);
  border-radius:9px;background:var(--surface);color:var(--accent);font-size:12px;
  font-weight:700;white-space:nowrap;cursor:pointer;overflow:hidden;
}
.entity-list-table .photo-upload-action input[type=file]{
  position:absolute;inset:0;width:100%;height:100%;margin:0;padding:0;
  opacity:0;cursor:pointer;font-size:0;
}
.entity-list-table .photo-upload-action:focus-within{outline:2px solid var(--accent);outline-offset:2px}
.entity-list-table td:first-child .entity-photo{display:block;width:78px;height:78px;object-fit:cover;border-radius:10px}
.entity-list-table td:first-child .muted{display:block}
.entity-list-table tr[hidden]{display:none!important}
@media(max-width:600px){
  .entity-list-table{overflow:visible;border:0;background:transparent;box-shadow:none}
  .entity-list-table table{display:block;min-width:0;width:100%}
  .entity-list-table thead{display:none}
  .entity-list-table tbody{display:grid;gap:12px}
  .entity-list-table tbody tr[data-classification]{
    display:grid;grid-template-columns:96px minmax(0,1fr);gap:7px 12px;
    padding:14px;border:1px solid var(--border);border-radius:14px;
    background:var(--surface);box-shadow:0 1px 3px #0001;
  }
  .entity-list-table td{display:block;min-width:0;padding:0;border:0;overflow-wrap:anywhere;font-size:13px}
  .entity-list-table td:nth-child(1){grid-row:1 / span 2;grid-column:1;align-self:start}
  .entity-list-table td:nth-child(2){grid-row:1;grid-column:2;font-weight:800;font-size:16px;line-height:1.35;color:var(--text)}
  .entity-list-table td:nth-child(3){grid-row:2;grid-column:2;color:var(--muted)}
  .entity-list-table td:nth-child(n+4):not(:last-child){display:flex;align-items:baseline;justify-content:space-between;gap:10px;grid-column:1/-1;border-top:1px solid var(--border-soft);padding-top:8px;text-align:right}
  .entity-list-table td:nth-child(n+4):not(:last-child)::before{flex:0 0 auto;color:var(--muted);font-size:11px;text-align:left}
  .preparation-list td:nth-child(4)::before{content:"Rendimiento"}
  .preparation-list td:nth-child(5)::before{content:"Costo total"}
  .preparation-list td:nth-child(6)::before{content:"Costo por unidad"}
  .entity-list-table td:last-child{grid-column:1/-1;display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:6px;padding-top:10px;border-top:1px solid var(--border-soft)}
  .entity-list-table td:last-child .link{display:block;width:100%;min-height:40px;padding:9px 5px;text-align:center;border:1px solid var(--border);border-radius:8px}
  .entity-list-table td:first-child .entity-photo{width:88px;height:88px}
  .entity-list-table .photo-upload-action{width:88px;min-height:38px;font-size:11px;padding:6px 2px;white-space:normal;text-align:center}
  .entity-list-table tbody tr:not([data-classification]){display:block;padding:14px;border:1px solid var(--border);border-radius:12px}
  .entity-list-table tbody tr:not([data-classification]) td{display:block}
  .title-actions{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));width:100%;gap:8px}
  .title-actions .primary{min-height:44px;min-width:0;padding:10px 7px;font-size:13px}
  .title-actions .primary:last-child:nth-child(odd){grid-column:1/-1}
}

/* Universal compact mobile tables. Labels come from the actual column headers. */
@media (max-width:600px){
  #content .mobile-card-table{overflow:visible;max-width:100%;border:0;background:transparent;box-shadow:none}
  #content .mobile-card-table table{display:block;min-width:0;width:100%;table-layout:auto}
  #content .mobile-card-table thead{display:none}
  #content .mobile-card-table tbody{display:grid;gap:12px}
  #content .mobile-card-table tbody tr{
    display:grid;grid-template-columns:minmax(0,1fr);gap:0;
    padding:12px 14px;border:1px solid var(--border);border-radius:14px;
    background:var(--surface);box-shadow:0 1px 3px #0001;min-width:0;
  }
  #content .mobile-card-table tbody tr[hidden]{display:none!important}
  #content .mobile-card-table tbody td,
  #content .mobile-card-table tbody td:nth-child(n){
    display:flex;grid-row:auto;grid-column:1;align-items:flex-start;
    justify-content:space-between;gap:12px;min-width:0;width:100%;
    padding:9px 0;border:0;border-top:1px solid var(--border-soft);
    text-align:right;overflow-wrap:anywhere;word-break:normal;font-size:13px;
    color:var(--text);background:transparent;
  }
  #content .mobile-card-table tbody td::before,
  #content .mobile-card-table tbody td:nth-child(n)::before{
    content:attr(data-mobile-label);display:block;flex:0 0 38%;
    color:var(--muted);font-size:12px;font-weight:500;text-align:left;
    white-space:normal;
  }
  #content .mobile-card-table tbody td:first-child{border-top:0}
  #content .mobile-card-table tbody td:first-child:not(.mobile-checkbox-cell){
    font-weight:650;
  }
  #content .mobile-card-table tbody td input:not([type=checkbox]),
  #content .mobile-card-table tbody td select{
    max-width:100%;min-width:0;width:100%;font-size:16px;
  }
  #content .mobile-card-table tbody td .restaurant-target{
    display:flex;flex-wrap:wrap;justify-content:flex-end;gap:6px;min-width:0;
  }
  #content .mobile-card-table tbody td .restaurant-target input{width:72px;flex:0 0 72px}
  #content .mobile-card-table tbody td .badge{max-width:100%;white-space:normal;overflow-wrap:anywhere}
  #content .mobile-card-table tbody td.mobile-actions-cell{
    display:flex;flex-wrap:wrap;align-items:center;justify-content:flex-start;gap:8px;
    padding-top:12px;text-align:left;
  }
  #content .mobile-card-table tbody td.mobile-actions-cell::before{display:none}
  #content .mobile-card-table tbody td.mobile-actions-cell .link{
    display:inline-flex;justify-content:center;align-items:center;min-height:40px;
    padding:7px 11px;border:1px solid var(--border);border-radius:8px;
  }
  #content .mobile-card-table tbody td.mobile-checkbox-cell{
    justify-content:flex-start;align-items:center;gap:12px;
  }
  #content .mobile-card-table tbody td.mobile-checkbox-cell::before{flex:0 0 auto}
  #content .mobile-card-table tbody td.mobile-checkbox-cell input{width:22px;height:22px}
  #content .mobile-card-table tbody td .entity-photo{width:80px;height:80px}
  #content .mobile-card-table tbody td .photo-upload-action{width:auto;max-width:120px}
  #content .mobile-card-table tbody td:has(.entity-photo),
  #content .mobile-card-table tbody td:has(.photo-upload-action){
    display:flex;flex-direction:column;align-items:flex-start;text-align:left;gap:7px;
  }
  #content .mobile-card-table tbody td:has(.entity-photo)::before,
  #content .mobile-card-table tbody td:has(.photo-upload-action)::before{display:none}
  #content .mobile-card-table tbody td .classification-codes,
  #content .mobile-card-table tbody td .classification-orders{display:flex;flex-wrap:wrap;justify-content:flex-end;gap:5px}
  #content .mobile-card-table tbody td .restaurant-logo{max-width:90px;max-height:70px}
  #content .mobile-card-table tbody td input[type=file]{max-width:100%;font-size:12px}
  #content .mobile-card-table tbody td .link.ingredient-origin-link{text-align:right;white-space:normal}
  #content .mobile-card-table tbody td:has(.ingredient-origin-link){align-items:flex-start}
  #content .mobile-card-table tbody td .ingredient-origin-link{overflow-wrap:anywhere}
  #content .mobile-card-table tbody td.mobile-actions-cell:empty{display:none}
  #content .mobile-card-table tbody tr:not([data-classification]) td{display:flex}
  #content .mobile-card-table tbody tr:not([data-classification]) td.mobile-actions-cell:empty{display:none}
  #content .mobile-card-table tbody tr:not([data-classification]){display:grid}
  #content .mobile-view-suppliers tbody td:first-child,
  #content .mobile-view-prices tbody td:nth-child(2),
  #content .mobile-view-ingredients tbody td:first-child{
    font-size:15px;font-weight:700;
  }
  #content .mobile-view-suppliers tbody td:first-child::before,
  #content .mobile-view-prices tbody td:nth-child(2)::before,
  #content .mobile-view-ingredients tbody td:first-child::before{font-size:12px;font-weight:500}
  #content .mobile-view-suppliers tbody tr{padding:8px 14px}
  #content .mobile-view-suppliers tbody td{padding:7px 0}
  #content .bulk-price-actions{flex-wrap:wrap;gap:8px}
  #content .bulk-price-actions button{max-width:100%;white-space:normal}
}

/* Mobile summary first; secondary cost details remain available on demand. */
@media(max-width:600px){
  #content .mobile-card-table tbody tr.mobile-card-compact td.mobile-card-extra{display:none}
  #content .mobile-card-table tbody tr.mobile-card-compact.mobile-card-expanded td.mobile-card-extra{display:flex}
  #content .mobile-card-table tbody tr.mobile-card-compact td.mobile-actions-cell{
    display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px;
  }
  #content .mobile-card-table tbody tr.mobile-card-compact td.mobile-actions-cell .link,
  #content .mobile-card-table tbody tr.mobile-card-compact td.mobile-actions-cell .mobile-card-toggle{
    display:flex;align-items:center;justify-content:center;min-width:0;width:100%;
    min-height:42px;padding:8px 6px;border:1px solid var(--border);border-radius:9px;
    background:transparent;font:inherit;font-size:13px;font-weight:650;text-align:center;
  }
  #content .mobile-card-table tbody tr.mobile-card-compact td.mobile-actions-cell .mobile-card-toggle{
    grid-column:1/-1;color:var(--accent);cursor:pointer;
  }
  #content .mobile-card-table tbody tr.mobile-card-compact td.mobile-actions-cell .danger{color:var(--danger)}
  #content .mobile-card-table tbody tr.mobile-card-compact td.mobile-actions-cell .link:last-child:nth-child(even){grid-column:1/-1}
  #content .mobile-card-table tbody tr.mobile-card-compact td:first-child{
    min-height:0;gap:6px;
  }
  #content .mobile-card-table tbody tr.mobile-card-compact td:first-child img.entity-photo{
    width:84px;height:84px;object-fit:cover;border-radius:10px;
  }
  #content .mobile-card-table tbody tr.mobile-card-compact td:first-child img.mobile-photo-unavailable{display:none}
  #content .mobile-card-table tbody tr.mobile-card-compact .mobile-photo-fallback{
    display:block;max-width:180px;font-size:12px;
  }
}

/* iPhone recipe/preparation cards: photo and name share the first row. */
@media(max-width:600px){
  #content .entity-list-table.mobile-card-table tbody tr.mobile-card-compact{
    grid-template-columns:96px minmax(0,1fr);
    column-gap:12px;
    align-items:start;
  }
  #content .entity-list-table.mobile-card-table tbody tr.mobile-card-compact td:nth-child(1){
    grid-column:1;grid-row:1;display:flex;flex-direction:column;
    align-items:flex-start;justify-content:flex-start;gap:0;
    padding:0;border:0;min-width:0;
  }
  #content .entity-list-table.mobile-card-table tbody tr.mobile-card-compact td.mobile-card-name{
    grid-column:2;grid-row:1;display:block;width:100%;
    padding:2px 0 0;border:0;text-align:left;
    font-size:16px;font-weight:800;line-height:1.35;
    overflow-wrap:anywhere;
  }
  #content .entity-list-table.mobile-card-table tbody tr.mobile-card-compact td.mobile-card-name::before{display:none}
  #content .entity-list-table.mobile-card-table tbody tr.mobile-card-compact td:not(:first-child):not(.mobile-card-name){
    grid-column:1/-1;grid-row:auto;
  }
  #content .entity-list-table.mobile-card-table tbody tr.mobile-card-compact td:first-child img.entity-photo{
    width:88px;height:88px;
  }
  #content .entity-list-table.mobile-card-table tbody tr.mobile-card-compact:not(.mobile-card-expanded) .photo-upload-action{
    display:none;
  }
  #content .entity-list-table.mobile-card-table tbody tr.mobile-card-compact td.mobile-actions-cell .link{
    display:none;
  }
  #content .entity-list-table.mobile-card-table tbody tr.mobile-card-compact.mobile-card-expanded td.mobile-actions-cell .link{
    display:flex;
  }
  #content .entity-list-table.mobile-card-table tbody tr.mobile-card-compact td.mobile-actions-cell .mobile-card-toggle{
    grid-column:1/-1;
  }
}

/* Acceptance fixes: desktop list readability and reachable row actions. */
@media(min-width:601px){
  .price-detail-layout:has(> .price-list-pane > .entity-list-table){margin-top:18px}
  .price-detail-layout > .price-list-pane > .entity-list-table{margin-top:0}
  #content .entity-list-table{max-width:100%;overflow-x:auto;overscroll-behavior-x:contain}
  #content .entity-list-table table{width:max-content;min-width:100%;table-layout:auto}
  #content .entity-list-table th,
  #content .entity-list-table td{padding:11px 10px}
  #content .entity-list-table th:nth-child(2),
  #content .entity-list-table td:nth-child(2){min-width:190px;max-width:300px;white-space:normal;overflow-wrap:normal;word-break:normal;hyphens:none}
  #content .entity-list-table tbody tr{--entity-row-background:var(--surface);background:var(--entity-row-background)}
  #content .entity-list-table tbody tr:hover{--entity-row-background:var(--surface-hover)}
  #content .entity-list-table tbody tr.warn{--entity-row-background:color-mix(in srgb,var(--surface),#5b3a1d 14%)}
  #content .entity-list-table tbody tr:has(.record-detail-trigger[aria-expanded=true]){--entity-row-background:var(--surface-hover)}
  #content .entity-list-table th:last-child,
  #content .entity-list-table td:last-child{position:sticky;right:0;z-index:1;box-shadow:-5px 0 10px -8px #18211d80;min-width:240px}
  #content .entity-list-table th:last-child{z-index:2;background:var(--surface-soft)}
  #content .entity-list-table td:last-child{background:var(--entity-row-background,var(--surface))}
  #content .entity-list-table .mobile-card-toggle{display:none}
}


/* Query tables become complete cards in narrow dialogs, including routes/actions. */
@media(max-width:600px){
 #modal .dialog-card-table{overflow:visible;min-width:0;border:0;background:transparent}
 #modal .dialog-card-table table,#modal .dialog-card-table tbody{display:block;width:100%;min-width:0;table-layout:auto}
 #modal .dialog-card-table thead{display:none}
 #modal .dialog-card-table tbody tr{display:block;border:1px solid var(--border,#827260);border-radius:12px;margin:10px 0;padding:10px;min-width:0}
 #modal .dialog-card-table tbody td{display:grid;grid-template-columns:minmax(0,35%) minmax(0,1fr);gap:10px;width:100%;min-width:0;max-width:none;white-space:normal;overflow-wrap:anywhere;text-align:left;border:0;padding:8px 0;position:static;background:transparent;box-shadow:none}
 #modal .dialog-card-table tbody td::before{content:attr(data-label);font-weight:600;color:var(--muted,#a99b8b)}
 #modal .dialog-card-table tbody td .link{min-width:0;white-space:normal}
 #modal #simulationDownload,#modal #simulationExcel{display:block;min-height:44px;width:100%;margin-top:8px}
}
