:root {
  --bg: #0f1115;
  --panel: rgba(20, 23, 30, 0.88);
  --panel-strong: rgba(20, 23, 30, 0.96);
  --line: rgba(255, 255, 255, 0.12);
  --text: #f4f0e8;
  --muted: #a7adb8;
  --accent: #f0c85a;
  --accent-2: #5bd6b6;
  --danger: #ff6b5f;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  color: var(--text);
  background: var(--bg);
  font-family: "Trebuchet MS", Verdana, sans-serif;
  position: fixed;
  inset: 0;
  -webkit-user-select: none;
  overscroll-behavior: none;
  touch-action: none;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  position: fixed;
  inset: 0;
  display: grid;
  min-width: 100vw;
  min-height: 100dvh;
  height: 100dvh;
  overflow: hidden;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background:
    radial-gradient(circle at 25% 0%, rgba(240, 200, 90, 0.16), transparent 28rem),
    linear-gradient(180deg, #171b22 0%, #0f1115 58%);
}

.topbar {
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 58px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  border-bottom: 1px solid var(--line);
  background: rgba(10, 12, 16, 0.84);
  backdrop-filter: blur(18px);
}

.topbar strong,
.topbar span {
  display: block;
}

.topbar strong {
  font-size: 0.96rem;
}

.topbar span {
  max-width: 62vw;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.76rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mode-button,
.tool-button,
.primary-button {
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--text);
  cursor: pointer;
  background: rgba(255, 255, 255, 0.08);
}

.mode-button {
  min-width: 112px;
  min-height: 40px;
  padding: 8px 14px;
  font-weight: 800;
}

.mode-button[aria-pressed="true"] {
  color: #1b1608;
  border-color: transparent;
  background: var(--accent);
}

.stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.empty-state {
  position: absolute;
  inset: 22px;
  display: grid;
  place-items: center;
  padding: 22px;
  color: var(--muted);
  text-align: center;
  border: 1px dashed var(--line);
  border-radius: 26px;
}

.bottom-sheet {
  z-index: 6;
  display: grid;
  gap: 10px;
  max-height: 34dvh;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--panel);
  backdrop-filter: blur(20px);
}

.tool-row,
.action-row,
.compact-grid {
  display: grid;
  gap: 8px;
}

.tool-row {
  grid-template-columns: 1.2fr 0.6fr 0.6fr 1fr 0.9fr;
}

.compact-grid {
  grid-template-columns: 1fr 76px 58px;
}

.action-row {
  grid-template-columns: 1fr 1fr;
}

.tool-button,
.primary-button {
  min-height: 38px;
  padding: 8px 10px;
  font-size: 0.86rem;
}

.tool-button.danger {
  color: #ffe7e4;
  border-color: rgba(255, 107, 95, 0.36);
}

.primary-button {
  color: #0d1613;
  border: 0;
  font-weight: 800;
  background: var(--accent-2);
}

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

.brush-control {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 10px;
  align-items: center;
  color: var(--muted);
  font-size: 0.84rem;
}

input,
select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 8px 10px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
  outline: none;
}

input[type="color"] {
  padding: 4px;
}

input[type="range"] {
  min-height: 28px;
  padding: 0;
}

.field-form,
.ocr-panel,
.fields-panel {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.045);
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 0.8rem;
}

.section-title span {
  color: var(--text);
  font-weight: 800;
}

.ocr-matches,
.fields-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.ocr-chip,
.field-chip {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 10px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
}

.ocr-chip {
  cursor: pointer;
}

.ocr-chip.active {
  color: #1b1608;
  border-color: transparent;
  background: var(--accent);
}

.field-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
}

.field-chip button {
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 999px;
  color: #1b1608;
  background: var(--danger);
}

@media (min-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr 390px;
    grid-template-rows: auto 1fr;
  }

  .topbar {
    grid-column: 1 / -1;
  }

  .bottom-sheet {
    max-height: none;
    height: 100%;
    border-top: 0;
    border-left: 1px solid var(--line);
  }
}

@media (max-width: 899px) and (orientation: portrait) {
  .topbar {
    min-height: 48px;
    padding: calc(6px + env(safe-area-inset-top)) 10px 6px;
  }

  .mode-button {
    min-height: 34px;
    min-width: 94px;
    padding: 6px 10px;
  }

  .bottom-sheet {
    max-height: 30dvh;
    gap: 7px;
    padding-top: 8px;
  }

  .field-form,
  .ocr-panel,
  .fields-panel {
    gap: 6px;
    padding: 8px;
  }

  input,
  select,
  .tool-button,
  .primary-button {
    min-height: 34px;
  }
}

@media (max-width: 420px) {
  .tool-row {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .tool-button {
    padding: 8px 6px;
    font-size: 0.78rem;
  }
}
