:root {
  --bg: #f4f2ed;
  --paper: #fffdf8;
  --ink: #1f2523;
  --muted: #68706c;
  --line: #d8d4ca;
  --line-strong: #aaa397;
  --accent: #2d6a4f;
  --accent-dark: #1f513d;
  --warn: #9a5b18;
  --shadow: 0 18px 44px rgba(31, 37, 35, 0.12);
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}

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

button,
input,
select,
textarea,
canvas {
  touch-action: manipulation;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: center;
  padding: 18px max(24px, env(safe-area-inset-right)) 18px max(24px, env(safe-area-inset-left));
  background: rgba(244, 242, 237, 0.94);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(12px);
}

.eyebrow {
  margin: 0 0 2px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 25px;
}

h2 {
  margin-bottom: 0;
  font-size: 18px;
}

h3 {
  margin-bottom: 16px;
  font-size: 16px;
}

.actions,
.signature-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

button {
  min-height: 48px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 0 18px;
  color: var(--ink);
  background: #fffaf0;
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
}

button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

button.primary:hover {
  background: var(--accent-dark);
}

.layout {
  display: grid;
  grid-template-columns: minmax(250px, 310px) minmax(0, 1000px);
  gap: 20px;
  width: min(100%, 1380px);
  margin: 0 auto;
  padding: 22px;
}

.history {
  align-self: start;
  position: sticky;
  top: 91px;
  max-height: calc(100vh - 112px);
  overflow: auto;
  padding: 16px;
  background: #fffaf2;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.history input {
  width: 100%;
  margin-bottom: 12px;
}

.history-list {
  display: grid;
  gap: 8px;
}

.settings {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.settings summary {
  min-height: 44px;
  display: flex;
  align-items: center;
  color: var(--accent-dark);
  font-weight: 800;
  cursor: pointer;
}

.settings label {
  margin: 8px 0;
}

.settings button {
  width: 100%;
}

.history-item {
  width: 100%;
  height: auto;
  min-height: 72px;
  padding: 12px;
  text-align: left;
  background: #fffdf8;
}

.history-item strong,
.history-item span {
  display: block;
}

.history-item span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.sheet {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.brand {
  display: flex;
  gap: 14px;
  align-items: center;
  padding: 22px;
  border-bottom: 2px solid var(--ink);
  overflow: hidden;
}

.brand p {
  margin-bottom: 2px;
  color: var(--muted);
  font-weight: 700;
  text-transform: uppercase;
  font-size: 12px;
}

.brand h2 {
  font-size: 28px;
  text-transform: uppercase;
}

.company-logo {
  display: block;
  flex: 0 0 auto;
  width: clamp(150px, 20vw, 220px) !important;
  height: auto;
  max-height: 74px;
  object-fit: contain;
}

.section {
  padding: 20px 22px;
  border-bottom: 1px solid var(--line);
}

.section:last-child {
  border-bottom: 0;
}

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

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

label {
  display: grid;
  gap: 5px;
}

.label-head {
  display: flex;
  min-height: 26px;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

label span,
.field-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.pin-field {
  min-width: 34px;
  min-height: 30px;
  padding: 0 7px;
  border-radius: 6px;
  color: var(--muted);
  background: #fff;
  font-size: 13px;
  line-height: 1;
}

.pin-field.is-pinned {
  border-color: var(--accent);
  color: #fff;
  background: var(--accent);
}

label.has-pinned input,
label.has-pinned select,
label.has-pinned textarea {
  border-color: var(--accent);
  background: #f7fff9;
}

input,
select,
textarea {
  width: 100%;
  min-height: 52px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 10px 12px;
  color: var(--ink);
  background: #fff;
  font-size: 16px;
}

textarea {
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  outline: 3px solid rgba(45, 106, 79, 0.18);
  border-color: var(--accent);
}

.measure-table {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin: 18px 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbf8f1;
}

.measure-title {
  grid-column: 1 / -1;
  font-weight: 800;
}

.signature-block {
  margin-top: 16px;
}

canvas {
  display: block;
  width: 100%;
  height: 230px;
  margin-top: 6px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #fff;
  touch-action: none;
}

.signature-actions {
  margin-top: 8px;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 10;
  max-width: 340px;
  padding: 12px 14px;
  border-radius: 6px;
  background: var(--ink);
  color: #fff;
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.visible {
  opacity: 1;
  transform: translateY(0);
}

.hidden-submit {
  position: fixed;
  width: 1px;
  height: 1px;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

@media (max-width: 920px) {
  .topbar {
    position: sticky;
    align-items: stretch;
    flex-direction: column;
    padding: 14px;
  }

  h1 {
    font-size: 22px;
  }

  .actions {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .actions button {
    padding: 0 8px;
  }

  .layout {
    grid-template-columns: 1fr;
    padding: 14px;
    gap: 14px;
  }

  .history {
    position: static;
    max-height: none;
    box-shadow: none;
  }

  .history-list {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  }

  .grid,
  .grid.three,
  .measure-table {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .brand,
  .section {
    padding: 18px;
  }

  canvas {
    height: 280px;
  }
}

@media (max-width: 640px) {
  .actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .grid,
  .grid.three,
  .measure-table {
    grid-template-columns: 1fr;
  }

  .brand h2 {
    font-size: 22px;
  }

  .company-logo {
    width: 148px;
  }
}

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

  body {
    background: #fff;
    font-size: 10.5px;
  }

  .no-print,
  .topbar,
  .history,
  .toast {
    display: none !important;
  }

  .layout {
    display: block;
    width: auto;
    margin: 0;
    padding: 0;
  }

  .sheet {
    border: 1px solid #222;
    border-radius: 0;
    box-shadow: none;
  }

  .brand,
  .section {
    padding: 8px 10px;
  }

  .brand h2 {
    font-size: 20px;
  }

  h3 {
    margin-bottom: 7px;
    font-size: 12px;
  }

  .grid,
  .grid.three,
  .measure-table {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .measure-table {
    margin: 8px 0;
    padding: 8px;
    border-radius: 0;
  }

  label span,
  .field-label,
  .pin-field {
    font-size: 8px;
  }

  .pin-field {
    display: none;
  }

  input,
  select,
  textarea {
    min-height: 25px;
    border-color: #333;
    border-radius: 0;
    padding: 3px 5px;
    appearance: none;
    color: #000;
  }

  canvas {
    height: 90px;
    border-radius: 0;
  }
}
