:root {
  --page: #f6f7f5;
  --surface: #ffffff;
  --surface-strong: #eef2ef;
  --line: #c9ced1;
  --line-soft: #e5e8ea;
  --text: #171a1f;
  --muted: #5d6670;
  --accent: #2f6f5e;
  --accent-strong: #235547;
  --danger: #9d2c2c;
  --warning-bg: #fff1c4;
  --missing-bg: #fde3dc;
  --ok-bg: #e6f4e8;
  --shadow: 0 12px 30px rgba(24, 32, 38, 0.08);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--page);
  -webkit-text-size-adjust: 100%;
}

body {
  min-width: 320px;
  margin: 0;
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 12px;
  line-height: 1.35;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

button:hover {
  border-color: #8e979f;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
.home-assistant-link:focus-visible {
  outline: 3px solid rgba(47, 111, 94, 0.28);
  outline-offset: 2px;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 14px max(16px, env(safe-area-inset-right)) 14px max(16px, env(safe-area-inset-left));
  border-bottom: 8px solid #151515;
  background: #def3d3;
}

.app-brand {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 16px;
}

.home-assistant-link {
  display: inline-flex;
  width: 58px;
  height: 58px;
  flex: 0 0 58px;
  align-items: center;
  justify-content: center;
  border: 2px solid rgba(21, 21, 21, 0.18);
  border-radius: 14px;
  background: #18a2d9;
  color: #ffffff;
  text-decoration: none;
  box-shadow: 0 5px 14px rgba(24, 162, 217, 0.24);
}

.home-assistant-link:hover {
  background: #0789bd;
}

.home-assistant-icon {
  display: block;
  width: 38px;
  height: 38px;
}

.app-header h1 {
  margin: 0;
  font-size: 1.6rem;
  line-height: 1.05;
}

.stock-summary {
  min-height: 18px;
  margin: 5px 0 0;
  color: #334039;
  font-weight: 400;
}

.save-status {
  min-width: 130px;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.74);
  color: #25322c;
  font-size: 0.9rem;
  font-weight: 600;
  text-align: center;
}

.save-status[data-state="error"] {
  background: #ffe1dc;
  color: var(--danger);
}

.save-status[data-state="saving"] {
  background: #fff4cf;
}

.app-shell {
  width: min(1460px, 100%);
  margin: 0 auto;
  padding: 10px clamp(10px, 2.5vw, 28px) 28px;
}

.tools-band {
  display: grid;
  grid-template-columns: minmax(170px, 250px) minmax(145px, 190px) auto;
  gap: 8px;
  align-items: end;
  padding: 8px 0 10px;
}

.field-group {
  display: grid;
  gap: 5px;
}

.field-group label {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
}

input,
select {
  min-height: 30px;
  padding: 4px 7px;
}

textarea {
  resize: vertical;
}

.toolbar-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.primary-button,
.secondary-button {
  padding: 0 10px;
  font-weight: 600;
  white-space: nowrap;
}

.primary-button {
  border-color: var(--accent);
  background: var(--accent);
  color: #ffffff;
}

.primary-button:hover {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

.secondary-button {
  background: var(--surface-strong);
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 360px);
  gap: 18px;
  align-items: start;
}

.stock-table-section,
.shopping-panel,
.category-section {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.table-wrap {
  overflow: auto;
  border-radius: 8px;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  min-width: 910px;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--line-soft);
  padding: 3px 5px;
  vertical-align: middle;
}

th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #e8e8e8;
  color: #111111;
  font-size: 0.78rem;
  text-align: left;
}

tbody tr.stock-row {
  background: color-mix(in srgb, var(--category-color, #dfe4e8) 32%, white);
}

tbody tr.stock-row:hover {
  background: color-mix(in srgb, var(--category-color, #dfe4e8) 44%, white);
}

.category-cell {
  min-width: 145px;
}

.category-select {
  font-weight: 400;
}

.stock-row input,
.stock-row select {
  background: rgba(255, 255, 255, 0.68);
  border-color: rgba(99, 107, 116, 0.38);
}

.expiry-input {
  min-width: 76px;
  text-align: center;
}

.name-input {
  min-width: 190px;
  font-weight: 400;
}

.number-input {
  width: 54px;
  min-width: 54px;
  text-align: center;
  font-weight: 900;
}

.quantity-controls {
  display: grid;
  grid-template-columns: 30px 54px 30px;
  gap: 3px;
  align-items: center;
}

.icon-button {
  width: 30px;
  min-width: 30px;
  padding: 0;
  font-size: 1rem;
  font-weight: 900;
  line-height: 1;
}

.minus-button {
  background: #f2f3f4;
}

.plus-button {
  border-color: #8ab39f;
  background: #e5f2eb;
  color: #173f31;
}

.delete-button {
  border-color: #deb7b1;
  background: #fff0ee;
  color: var(--danger);
}

.bare-delete-button {
  border: 0;
  background: transparent;
  color: #d00000;
  font-size: 1.25rem;
  font-weight: 900;
}

.bare-delete-button:hover {
  border: 0;
  background: transparent;
  color: #ff0000;
}

.balance-count {
  display: inline-flex;
  min-width: 35px;
  min-height: 24px;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  background: var(--ok-bg);
  font-weight: 900;
}

.balance-count.is-missing {
  background: var(--missing-bg);
  color: var(--danger);
}

.balance-count.is-surplus {
  background: #00ff2a;
  color: #06330d;
  box-shadow: inset 0 0 0 1px rgba(0, 106, 21, 0.35);
}

.control-cell {
  min-width: 100px;
  text-align: center;
  white-space: nowrap;
}

.control-check {
  display: inline-flex;
  width: 20px;
  height: 20px;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: #00d336;
  color: #ffffff;
  font-weight: 900;
  line-height: 1;
  vertical-align: middle;
}

.control-date {
  display: inline-block;
  margin-left: 4px;
  color: #26302b;
  font-size: 0.9rem;
  vertical-align: middle;
}

.control-date.is-stale,
.control-needed {
  color: #6d757d;
}

.empty-state {
  margin: 0;
  padding: 16px;
  color: var(--muted);
  font-weight: 600;
  text-align: center;
}

.shopping-panel {
  padding: 10px;
}

.panel-header,
.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panel-header h2,
.section-heading h2 {
  margin: 0;
  font-size: 1rem;
}

#shoppingList {
  min-height: 380px;
  margin-top: 12px;
  padding: 8px;
  font-family: Consolas, "Courier New", monospace;
  font-size: 1rem;
  line-height: 1.4;
}

.print-shopping-list {
  display: none;
}

.panel-status {
  min-height: 18px;
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
}

.category-section {
  margin-top: 18px;
  padding: 10px;
}

.category-editor {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.category-row {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) 30px;
  gap: 6px;
  align-items: center;
  padding: 8px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: #fbfbfb;
}

.category-row input[type="color"] {
  width: 36px;
  min-width: 36px;
  height: 30px;
  padding: 2px;
}

.category-row input[type="text"] {
  font-weight: 400;
}

.item-dialog {
  width: min(520px, calc(100vw - 24px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 22px 70px rgba(15, 23, 30, 0.28);
}

.item-dialog::backdrop {
  background: rgba(18, 24, 30, 0.32);
}

.item-form {
  display: grid;
  gap: 14px;
  padding: 14px;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line-soft);
}

.dialog-header h2 {
  margin: 0;
  font-size: 1.05rem;
}

.dialog-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.dialog-grid .field-group:nth-child(2) {
  grid-column: 1 / -1;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* iPadOS et tablettes en mode paysage */
@media (orientation: landscape) and (min-width: 768px) and (max-width: 1366px) {
  body {
    font-size: 13px;
  }

  .app-header {
    min-height: 86px;
    gap: 12px;
    padding-top: 10px;
    padding-bottom: 10px;
    border-bottom-width: 6px;
  }

  .app-brand {
    gap: 13px;
  }

  .home-assistant-link {
    width: 54px;
    height: 54px;
    flex-basis: 54px;
    border-radius: 13px;
  }

  .home-assistant-icon {
    width: 36px;
    height: 36px;
  }

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

  .stock-summary {
    font-size: 0.95rem;
  }

  .save-status {
    min-width: 120px;
    min-height: 38px;
    padding: 9px 12px;
    font-size: 0.95rem;
  }

  .app-shell {
    padding: 8px 10px 24px;
  }

  .tools-band {
    position: sticky;
    top: 0;
    z-index: 20;
    grid-template-columns: minmax(250px, 1.35fr) minmax(180px, 0.8fr) auto;
    gap: 10px;
    margin: 0 -2px;
    padding: 9px 2px 10px;
    background: var(--page);
  }

  .field-group label {
    font-size: 0.76rem;
  }

  input,
  select,
  button {
    min-height: 42px;
    font-size: 16px;
  }

  input,
  select {
    padding: 6px 9px;
  }

  .toolbar-actions {
    gap: 8px;
  }

  .primary-button,
  .secondary-button {
    padding-inline: 13px;
  }

  .content-grid {
    display: block;
  }

  /* Sur iPad, la liste destinée à l'impression ne prend plus 1/3 de l'écran. */
  .shopping-panel {
    display: none !important;
  }

  .stock-table-section {
    width: 100%;
  }

  .table-wrap {
    width: 100%;
    overflow-x: hidden;
  }

  table {
    width: 100%;
    min-width: 0;
    table-layout: fixed;
  }

  th,
  td {
    padding: 4px;
  }

  th {
    font-size: 0.72rem;
    line-height: 1.1;
    white-space: normal;
  }

  th:nth-child(1),
  td:nth-child(1) {
    width: 16%;
  }

  th:nth-child(2),
  td:nth-child(2) {
    width: 11%;
  }

  th:nth-child(3),
  td:nth-child(3) {
    width: 24%;
  }

  th:nth-child(4),
  td:nth-child(4) {
    width: 7%;
    text-align: center;
  }

  th:nth-child(5),
  td:nth-child(5) {
    width: 15%;
  }

  th:nth-child(6),
  td:nth-child(6) {
    width: 7%;
    text-align: center;
  }

  th:nth-child(7),
  td:nth-child(7) {
    width: 13%;
    text-align: center;
  }

  th:nth-child(8),
  td:nth-child(8) {
    width: 7%;
    text-align: center;
  }

  .category-cell,
  .category-select,
  .expiry-input,
  .name-input,
  .number-input,
  .control-cell {
    min-width: 0;
  }

  .stock-row input,
  .stock-row select {
    height: 40px;
    min-height: 40px;
    padding: 5px 7px;
    font-size: 15px;
  }

  .name-input {
    width: 100%;
  }

  .number-input {
    width: 100%;
    min-width: 0;
    padding-inline: 2px !important;
  }

  .quantity-controls {
    grid-template-columns: 38px minmax(38px, 1fr) 38px;
    gap: 4px;
  }

  .icon-button {
    width: 38px;
    min-width: 38px;
    height: 40px;
    min-height: 40px;
    font-size: 1.15rem;
  }

  .balance-count {
    min-width: 34px;
    min-height: 32px;
    font-size: 1rem;
  }

  .control-cell {
    white-space: normal;
  }

  .control-check {
    width: 25px;
    height: 25px;
  }

  .control-date {
    display: block;
    margin: 2px 0 0;
    font-size: 0.72rem;
    line-height: 1.05;
  }

  .bare-delete-button {
    width: 40px;
    min-width: 40px;
    height: 40px;
    min-height: 40px;
    font-size: 1.5rem;
  }

  .category-section {
    margin-top: 12px;
  }

  .category-editor {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .item-dialog {
    width: min(680px, calc(100vw - 36px));
  }

  .item-form {
    gap: 16px;
    padding: 18px;
  }

  .dialog-grid {
    gap: 12px;
  }
}

/* Tablettes étroites et téléphones */
@media (max-width: 767px), (orientation: portrait) and (max-width: 1024px) {
  body {
    font-size: 13px;
  }

  .app-header {
    gap: 10px;
    padding: 10px 12px;
    border-bottom-width: 6px;
  }

  .home-assistant-link {
    width: 50px;
    height: 50px;
    flex-basis: 50px;
  }

  .home-assistant-icon {
    width: 33px;
    height: 33px;
  }

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

  .save-status {
    min-width: 104px;
    padding-inline: 8px;
    font-size: 0.82rem;
  }

  .app-shell {
    padding: 8px;
  }

  .tools-band {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .toolbar-actions {
    grid-column: 1 / -1;
    justify-content: stretch;
  }

  .toolbar-actions button {
    min-height: 44px;
    flex: 1;
    font-size: 15px;
  }

  input,
  select {
    min-height: 44px;
    font-size: 16px;
  }

  .content-grid {
    display: block;
  }

  .shopping-panel {
    display: none !important;
  }

  .table-wrap {
    overflow-x: hidden;
  }

  table {
    width: 100%;
    min-width: 0;
    table-layout: fixed;
  }

  /* En petit écran, conserver uniquement les champs nécessaires au comptage. */
  th:nth-child(1),
  td:nth-child(1),
  th:nth-child(2),
  td:nth-child(2),
  th:nth-child(4),
  td:nth-child(4),
  th:nth-child(6),
  td:nth-child(6),
  th:nth-child(7),
  td:nth-child(7) {
    display: none;
  }

  th:nth-child(3),
  td:nth-child(3) {
    width: 48%;
  }

  th:nth-child(5),
  td:nth-child(5) {
    width: 39%;
  }

  th:nth-child(8),
  td:nth-child(8) {
    width: 13%;
    text-align: center;
  }

  th,
  td {
    padding: 4px;
  }

  .name-input,
  .number-input {
    min-width: 0;
  }

  .stock-row input,
  .stock-row select {
    height: 42px;
    min-height: 42px;
    font-size: 16px;
  }

  .quantity-controls {
    grid-template-columns: 38px minmax(38px, 1fr) 38px;
    gap: 3px;
  }

  .icon-button,
  .bare-delete-button {
    width: 38px;
    min-width: 38px;
    height: 42px;
    min-height: 42px;
  }

  .category-editor {
    grid-template-columns: 1fr;
  }

  .dialog-grid {
    grid-template-columns: 1fr;
  }

  .dialog-grid .field-group:nth-child(2) {
    grid-column: auto;
  }
}

@media (max-width: 520px) {
  .app-header {
    align-items: flex-start;
  }

  .app-brand {
    gap: 9px;
  }

  .save-status {
    min-width: 86px;
    max-width: 94px;
  }

  .tools-band {
    grid-template-columns: 1fr;
  }

  .toolbar-actions {
    grid-column: auto;
    flex-direction: column;
  }

  th:nth-child(3),
  td:nth-child(3) {
    width: 45%;
  }

  th:nth-child(5),
  td:nth-child(5) {
    width: 43%;
  }

  th:nth-child(8),
  td:nth-child(8) {
    width: 12%;
  }
}

@media print {
  @page {
    size: A4 portrait;
    margin: 8mm;
  }

  body {
    background: #ffffff;
    color: #000000;
    font-size: 8pt;
  }

  .app-header,
  .tools-band,
  .stock-table-section,
  .category-section,
  #shoppingList,
  .panel-header button,
  .panel-status {
    display: none !important;
  }

  .app-shell,
  .content-grid,
  .shopping-panel {
    display: block !important;
    width: 98mm;
    max-height: 140mm;
    margin: 0;
    padding: 0;
    border: 0;
    box-shadow: none;
  }

  .panel-header h2 {
    display: block;
    margin: 0 0 5mm;
    font-family: Consolas, "Courier New", monospace;
    font-size: 9pt;
    font-weight: 700;
  }

  .print-shopping-list {
    display: block;
    margin: 0;
    white-space: pre-wrap;
    font-family: Consolas, "Courier New", monospace;
    font-size: 8pt;
    line-height: 1.12;
  }
}
