:root {
  --bg: #eef1f4;
  --bg-accent: #e3e9ef;
  --ink: #15202b;
  --muted: #5b6b7c;
  --card: #ffffff;
  --line: #d7dee6;
  --line-soft: #e8eef3;
  --accent: #0d6e62;
  --accent-soft: #e6f4f1;
  --accent-ink: #ffffff;
  --warn: #9a4b16;
  --warn-soft: #fff1e6;
  --ok: #1b6b3a;
  --ok-soft: #e8f6ee;
  --danger: #a11f2c;
  --shadow: 0 1px 2px rgba(21, 32, 43, 0.04), 0 12px 28px rgba(21, 32, 43, 0.06);
  --radius: 12px;
  --font: "Plus Jakarta Sans", "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  background:
    radial-gradient(900px 420px at 8% -10%, rgba(13, 110, 98, 0.12), transparent 55%),
    radial-gradient(700px 380px at 100% 0%, rgba(47, 92, 140, 0.1), transparent 50%),
    linear-gradient(180deg, #f7f9fb 0%, var(--bg) 45%, var(--bg-accent) 100%);
}

.hidden {
  display: none !important;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: min(400px, 100%);
  padding: 28px;
  display: grid;
  gap: 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.brand {
  margin: 0;
  font-size: 1.55rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.muted {
  color: var(--muted);
  margin: 0;
}

input,
select,
button {
  font: inherit;
}

input,
select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  color: var(--ink);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input:focus,
select:focus {
  border-color: color-mix(in srgb, var(--accent) 55%, white);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

button {
  border: 0;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 600;
  transition: transform 0.12s ease, background 0.12s ease, opacity 0.12s ease;
}

button:hover {
  background: #0a5c52;
}

button:active {
  transform: translateY(1px);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

button.ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
  font-weight: 500;
}

button.ghost:hover {
  background: #f5f8fb;
}

.error {
  color: var(--danger);
  min-height: 1.2em;
  margin: 0;
  font-size: 0.92rem;
}

#app {
  width: min(1180px, calc(100% - 28px));
  margin: 22px auto 56px;
  display: grid;
  gap: 14px;
}

.top {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  padding: 18px 20px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.top h1 {
  margin: 0;
  font-size: 1.45rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.top .muted {
  margin-top: 4px;
  font-size: 0.92rem;
}

.tabs {
  display: flex;
  gap: 8px;
  width: fit-content;
  padding: 4px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--line);
  border-radius: 999px;
  backdrop-filter: blur(8px);
}

.tabs button {
  background: transparent;
  color: var(--muted);
  border: 0;
  border-radius: 999px;
  padding: 8px 14px;
  font-weight: 600;
}

.tabs button:hover {
  background: transparent;
  color: var(--ink);
}

.tabs button.active {
  background: var(--accent);
  color: var(--accent-ink);
}

.panel {
  display: grid;
  gap: 12px;
}

.filter-drawer {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.filter-summary {
  display: none;
  list-style: none;
  cursor: pointer;
  user-select: none;
  font-weight: 600;
  padding: 14px 14px;
  min-height: 44px;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.filter-summary::-webkit-details-marker {
  display: none;
}

.filter-summary::after {
  content: "▾";
  color: var(--muted);
  font-size: 0.85rem;
  transition: transform 0.15s ease;
}

.filter-drawer:not([open]) > .filter-summary::after {
  transform: rotate(-90deg);
}

.filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(148px, 1fr));
  gap: 8px;
  padding: 12px;
}

.filters button[type="submit"] {
  min-width: 96px;
}

.table-wrap {
  overflow: auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.94rem;
}

th,
td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line-soft);
  text-align: left;
  vertical-align: top;
}

th {
  position: sticky;
  top: 0;
  background: #f8fafc;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  border-bottom-color: var(--line);
}

tbody tr:hover td {
  background: #f7fbfa;
}

tbody tr:last-child td {
  border-bottom: 0;
}

.actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.actions a,
.actions button,
.actions span {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 52px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  text-decoration: none;
  color: var(--ink);
  font-size: 0.8rem;
  font-weight: 600;
  background: #fff;
}

.actions button {
  cursor: pointer;
  font: inherit;
}

.actions .icon-btn {
  min-width: 34px;
  width: 34px;
  height: 34px;
  padding: 0;
}

.actions .icon-btn svg {
  display: block;
}

.actions a:hover,
.actions button:hover {
  border-color: color-mix(in srgb, var(--accent) 40%, var(--line));
  background: var(--accent-soft);
  color: var(--accent);
}

.actions span.disabled {
  opacity: 0.4;
}

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: 18px;
}

.lightbox.hidden {
  display: none !important;
}

.lightbox-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 32, 0.55);
  backdrop-filter: blur(4px);
}

.lightbox-panel {
  position: relative;
  z-index: 1;
  width: min(920px, calc(100vw - 24px));
  height: min(92vh, 1100px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 24px 60px rgba(15, 23, 32, 0.28);
  overflow: hidden;
}

.lightbox-top {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: start;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line-soft);
  background: #f8fafc;
}

.lightbox-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.lightbox-top .muted {
  margin-top: 2px;
  font-size: 0.85rem;
}

.lightbox-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
}

.ghost-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  text-decoration: none;
  color: var(--ink);
  font-size: 0.88rem;
  font-weight: 600;
  background: #fff;
}

.ghost-link:hover {
  background: #f5f8fb;
}

.ghost-link.is-disabled {
  opacity: 0.4;
  pointer-events: none;
}

.lightbox-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.82);
  color: var(--muted);
  font-weight: 600;
  z-index: 2;
}

.preview-zoom-controls {
  display: inline-flex;
  gap: 6px;
  align-items: center;
}

.preview-zoom-controls .ghost {
  min-width: 40px;
  padding: 8px 10px;
}

.preview-stage {
  position: relative;
  grid-row: 2;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  background: #d8dee6;
  touch-action: none;
  cursor: grab;
}

.preview-stage.is-panning {
  cursor: grabbing;
}

.preview-paper {
  transform-origin: 0 0;
  width: 794px;
  margin: 0;
  will-change: transform;
}

#preview-frame {
  display: block;
  width: 794px;
  height: 1123px;
  border: 0;
  background: #fff;
  pointer-events: none;
  box-shadow: 0 8px 28px rgba(15, 23, 32, 0.18);
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 8px;
  border-radius: 7px;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.2;
  border: 1px solid transparent;
  white-space: nowrap;
}

.badge.ok {
  color: var(--ok);
  background: var(--ok-soft);
  border-color: color-mix(in srgb, var(--ok) 22%, white);
}

.badge.warn {
  color: var(--warn);
  background: var(--warn-soft);
  border-color: color-mix(in srgb, var(--warn) 22%, white);
}

.badge.neutral {
  color: var(--muted);
  background: #f1f4f7;
  border-color: var(--line);
}

.pager {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--muted);
  font-size: 0.9rem;
}

.mono {
  font-family: var(--mono);
  font-size: 0.82rem;
  letter-spacing: -0.01em;
}

.sub {
  color: var(--muted);
  font-size: 0.82rem;
  margin-top: 2px;
}

.amount {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  white-space: nowrap;
}

@media (max-width: 820px) {
  body {
    font-size: 15px;
  }

  #app {
    width: calc(100% - 16px);
    margin: 10px auto 28px;
    gap: 10px;
  }

  .login-view {
    padding: 16px;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }

  .login-card {
    padding: 22px 18px;
  }

  .top {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    padding: 14px 14px;
  }

  .top h1 {
    font-size: 1.25rem;
  }

  .top .muted {
    font-size: 0.86rem;
  }

  #logout {
    width: 100%;
    min-height: 44px;
  }

  .tabs {
    width: 100%;
  }

  .tabs button {
    flex: 1;
    min-height: 44px;
  }

  .filter-summary {
    display: flex;
  }

  .filter-drawer[open] > .filters {
    border-top: 1px solid var(--line-soft);
  }

  .filters {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    padding: 12px;
  }

  .filters input[name="q"],
  .filters input[name="company"],
  .filters input[name="product"],
  .filters button[type="submit"] {
    grid-column: 1 / -1;
  }

  .filters input,
  .filters select,
  .filters button {
    min-height: 44px;
    font-size: 16px; /* avoid iOS zoom on focus */
  }

  .tabs {
    position: sticky;
    top: 0;
    z-index: 20;
    width: 100%;
    background: rgba(247, 249, 251, 0.92);
  }

  .table-wrap {
    border: 0;
    background: transparent;
    box-shadow: none;
    overflow: visible;
  }

  table,
  thead,
  tbody,
  th,
  td,
  tr {
    display: block;
    width: 100%;
  }

  thead {
    display: none;
  }

  tbody {
    display: grid;
    gap: 10px;
  }

  tbody tr {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 12px 12px 10px;
  }

  tbody tr:hover td {
    background: transparent;
  }

  tbody td {
    display: grid;
    grid-template-columns: 96px 1fr;
    gap: 8px;
    align-items: start;
    padding: 8px 2px;
    border-bottom: 1px solid var(--line-soft);
  }

  tbody td:last-child {
    border-bottom: 0;
    padding-top: 12px;
  }

  tbody td.amount {
    font-size: 1.05rem;
    font-weight: 700;
  }

  tbody td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    padding-top: 2px;
  }

  tbody td.actions {
    display: grid;
    grid-template-columns: 48px 1fr 1fr;
    gap: 8px;
    align-items: stretch;
    padding-top: 12px;
  }

  tbody td.actions::before {
    grid-column: 1 / -1;
    margin-bottom: 0;
  }

  tbody td.actions a,
  tbody td.actions button,
  tbody td.actions span {
    width: 100%;
    min-height: 44px;
    min-width: 0;
  }

  tbody td.actions .icon-btn {
    width: 100%;
    min-width: 0;
    height: 44px;
  }

  .pager {
    position: sticky;
    bottom: 0;
    z-index: 15;
    gap: 8px;
    padding: 10px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    background: rgba(247, 249, 251, 0.94);
    backdrop-filter: blur(8px);
    border: 1px solid var(--line);
    border-radius: var(--radius);
  }

  .pager button {
    min-height: 44px;
    min-width: 84px;
    flex: 1;
  }

  #inv-page,
  #tk-page {
    text-align: center;
    font-size: 0.82rem;
    line-height: 1.3;
  }

  .login-card input,
  .login-card button {
    min-height: 44px;
    font-size: 16px;
  }

  .lightbox {
    padding: 0;
    place-items: stretch;
  }

  .lightbox-panel {
    width: 100%;
    height: 100%;
    max-height: none;
    border: 0;
    border-radius: 0;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }

  .lightbox-top {
    flex-direction: column;
    gap: 10px;
    padding: 12px 12px 10px;
  }

  .lightbox-actions {
    width: 100%;
    display: grid;
    grid-template-columns: auto 1fr 1fr auto;
    gap: 8px;
  }

  .preview-zoom-controls {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
  }

  .preview-zoom-controls .ghost,
  .ghost-link,
  .lightbox-actions .ghost {
    min-height: 44px;
  }

  .preview-stage {
    touch-action: none;
  }

  .preview-paper {
    margin: 10px;
  }

}

@media (max-width: 420px) {
  .filters {
    grid-template-columns: 1fr;
  }

  .filters input[name="q"],
  .filters input[name="company"],
  .filters input[name="product"],
  .filters button[type="submit"] {
    grid-column: auto;
  }

  .lightbox-actions {
    grid-template-columns: 1fr 1fr;
  }

  .lightbox-actions > #preview-close {
    grid-column: 1 / -1;
  }
}
