:root {
  --text: #6b6375;
  --text-h: #08060d;
  --bg: #fff;
  --surface: #f7f6f9;
  --border: #e5e4e7;
  --accent: #aa3bff;
  --accent-bg: rgba(170, 59, 255, 0.1);
  --danger: #d63b3b;
  --success: #1a8a4a;

  --sans: system-ui, 'Segoe UI', Roboto, sans-serif;
  --mono: ui-monospace, Consolas, monospace;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);

  font: 16px/145% var(--sans);
  color-scheme: light dark;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

@media (prefers-color-scheme: dark) {
  :root {
    --text: #9ca3af;
    --text-h: #f3f4f6;
    --bg: #16171d;
    --surface: #1c1d24;
    --border: #2e303a;
    --accent: #c084fc;
    --accent-bg: rgba(192, 132, 252, 0.15);
    --danger: #f87171;
    --success: #4ade80;
  }
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
}

#root {
  height: 100svh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

h1, h2 {
  color: var(--text-h);
  font-weight: 600;
}

button {
  font: inherit;
  cursor: pointer;
}

input, textarea {
  font: inherit;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100svh;
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
  overflow: hidden;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(12px, var(--safe-top)) 16px 12px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
  flex-shrink: 0;
}

.app-header h1 {
  font-size: 18px;
  margin: 0;
  white-space: nowrap;
}

.app-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100svh;
}

.tabs,
.browse-tabs {
  display: flex;
  gap: 4px;
}

.tabs button,
.browse-tabs button {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 10px 16px;
  min-height: 44px;
  border-radius: 8px;
}

.tabs button.active,
.browse-tabs button.active {
  background: var(--accent-bg);
  color: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
}

.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.scanner {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #000;
  position: relative;
  min-height: 0;
}

.scanner-video {
  width: 100%;
  max-height: 100%;
  object-fit: cover;
}

.scanner-message {
  color: #fff;
  padding: 16px;
  text-align: center;
}

.scanner-error {
  color: #ff8080;
}

.scanner-confirm {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 16px 16px calc(16px + var(--safe-bottom));
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.scanner-confirm-code {
  color: #fff;
  font-family: var(--mono);
  font-size: 15px;
  word-break: break-all;
  text-align: center;
  margin: 0;
}

.scanner-confirm-actions {
  display: flex;
  gap: 8px;
}

.scanner-confirm-actions button {
  flex: 1;
}

.scanner-confirm-actions button:not(.primary) {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.4);
  color: #fff;
  padding: 12px 18px;
  min-height: 44px;
  border-radius: 8px;
}

.setup {
  padding: 24px 16px calc(16px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 24px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.setup-io h2 {
  font-size: 15px;
  margin: 0 0 8px;
}

.setup-io > h2 + .browse-io {
  margin-bottom: 20px;
}

.setup-divider {
  border: none;
  border-top: 1px solid var(--border);
  width: 100%;
  margin: 0;
}

.browse {
  padding: 16px 16px calc(16px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.browse-io {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.browse-io button {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 8px 14px;
  min-height: 40px;
  border-radius: 6px;
  font-size: 14px;
}

.browse-io button.success {
  border-color: var(--success);
  color: var(--success);
}

.list-view {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.list-toolbar button.accent {
  width: 100%;
}

.item-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.item-row {
  width: 100%;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}

.item-row-primary {
  color: var(--text-h);
  font-weight: 600;
}

.item-row-secondary {
  font-size: 13px;
}

.hint {
  font-size: 14px;
  opacity: 0.8;
}

.item-form {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

/* Save/Cancel/Delete live outside the scrolling area entirely, right below
   the header — some mobile browsers (e.g. Arc) float a toolbar over the
   bottom of the page that doesn't scroll away and isn't reported via
   env(safe-area-inset-bottom), so nothing bottom-anchored can be relied on
   to ever be reachable. Top placement sidesteps that class of bug outright. */
.form-toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  flex-shrink: 0;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.form-fields {
  padding: 16px 16px calc(24px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.item-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--text-h);
}

.item-form input,
.item-form textarea {
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text-h);
}

.item-form input[readonly] {
  opacity: 0.7;
}

.form-row {
  display: flex;
  gap: 8px;
}

.form-row label {
  flex: 1;
  min-width: 0;
}

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

button.primary,
button.accent {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 12px 18px;
  min-height: 44px;
  border-radius: 8px;
  font-weight: 600;
}

button.danger {
  background: none;
  border: 1px solid var(--danger);
  color: var(--danger);
  padding: 12px 18px;
  min-height: 44px;
  border-radius: 8px;
  margin-left: auto;
}

.item-form button:not(.primary):not(.danger):not(.accent) {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 18px;
  min-height: 44px;
  border-radius: 8px;
}

.toast {
  position: fixed;
  bottom: calc(16px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-h);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  max-width: calc(100% - 32px);
  text-align: center;
}
