:root {
  color-scheme: light;
  --totp-page-bg: #f1f5f9;
  --totp-focus: #3157c8;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --color-bg: #1e293b;
    --color-bg-secondary: #1e293b;
    --color-bg-tertiary: #263449;
    --color-bg-elevated: #1e293b;
    --color-border: rgba(255, 255, 255, .12);
    --color-border-hover: rgba(255, 255, 255, .2);
    --color-text: #f1f5f9;
    --color-text-secondary: #b4c0d1;
    --color-text-muted: #94a3b8;
    --color-text-inverse: #0f172a;
    --color-primary: #9bb3ff;
    --color-primary-hover: #c1d0ff;
    --color-primary-light: #26365e;
    --color-primary-lighter: #26365e;
    --color-warning: #fbbf24;
    --totp-page-bg: #101827;
    --totp-focus: #9bb3ff;
}

body.totp-page {
  min-width: 320px;
  --color-bg: #ffffff;
  --color-bg-secondary: #f8fafc;
  --color-bg-elevated: #ffffff;
  background: var(--totp-page-bg);
  color: var(--color-text);
  font-family: var(--font-body);
}

[data-theme="dark"] body.totp-page {
    --color-bg: #1e293b;
    --color-bg-secondary: #263449;
    --color-bg-elevated: #1e293b;
}

.totp-page *,
.totp-page *::before,
.totp-page *::after { box-sizing: border-box; }

.skip-link {
  position: absolute;
  z-index: 700;
  top: var(--space-2);
  left: var(--space-2);
  padding: var(--space-3) var(--space-4);
  transform: translateY(-160%);
  border-radius: var(--radius-md);
  background: var(--color-bg);
  color: var(--color-text);
}

.skip-link:focus { transform: translateY(0); }

#toolUI { container-type: inline-size; }
.totp-workspace { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: var(--space-5); align-items: start; }
.totp-workspace__controls, .totp-workspace__output { min-width: 0; }
.totp-workspace__output { min-height: 0; overflow: hidden; border: 1px solid var(--color-border); border-radius: var(--radius-lg); background: var(--color-bg-secondary); }
.totp-code-empty { display: flex; flex-direction: column; min-height: 190px; padding: var(--space-4); }
.totp-code-empty h3 { margin: 0; color: var(--color-text); font-size: var(--text-lg); }
.totp-code-empty__body { display: grid; place-content: center; justify-items: center; gap: var(--space-2); flex: 1; color: var(--color-text-muted); text-align: center; }
.totp-code-empty__body svg { width: 36px; height: 36px; }
.totp-code-empty__body p { max-width: 28ch; margin: 0; color: var(--color-text-secondary); font-size: var(--text-sm); line-height: 1.5; }

.totp-mode-tabs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-2); margin-bottom: var(--space-5); padding: var(--space-1); border: 1px solid var(--color-border); border-radius: var(--radius-lg); background: var(--color-bg-secondary); }
.totp-mode-tab { min-height: 48px; padding: var(--space-2) var(--space-3); border: 0; border-radius: var(--radius-md); background: transparent; color: var(--color-text-secondary); font: inherit; font-size: var(--text-sm); font-weight: var(--font-semibold); cursor: pointer; }
.totp-mode-tab.is-active { background: var(--color-primary); color: var(--color-text-inverse); box-shadow: var(--shadow-subtle); }
.totp-mode-tab:hover:not(.is-active) { background: var(--color-primary-light); color: var(--color-primary); }
.totp-mode-note { margin: 0 0 var(--space-5); padding: var(--space-3) var(--space-4); border: 1px solid var(--color-border); border-radius: var(--radius-md); background: var(--color-bg-secondary); color: var(--color-text-secondary); font-size: var(--text-sm); line-height: 1.6; }

.totp-local-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--color-text-secondary);
  white-space: nowrap;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  background: var(--color-bg-secondary);
}

.totp-local-badge > span {
  width: .5rem;
  height: .5rem;
  border-radius: var(--radius-full);
  background: var(--color-success);
}

.totp-form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-4);
}

.totp-setup-warning { margin: var(--space-3) 0 0; padding: var(--space-3) var(--space-4); border-left: 3px solid var(--color-primary); border-radius: var(--radius-sm); background: var(--color-bg-secondary); color: var(--color-text-secondary); font-size: var(--text-sm); line-height: 1.6; }

.totp-label-options {
  margin: 0 0 var(--space-5);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-bg-secondary);
}

.totp-label-options summary {
  padding: var(--space-3) var(--space-4);
  color: var(--color-text-secondary);
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
  cursor: pointer;
}

.totp-label-options summary:hover { color: var(--color-primary); }
.totp-label-options[open] summary { color: var(--color-text); }
.totp-label-options .totp-form-grid { padding: 0 var(--space-4); }
.totp-label-options .totp-field { margin-bottom: var(--space-2); }
.totp-label-options p { margin: 0; padding: 0 var(--space-4) var(--space-4); color: var(--color-text-secondary); font-size: var(--text-xs); }

.totp-field {
  display: grid;
  min-width: 0;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
  color: var(--color-text);
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
}

.totp-field input,
.totp-field select {
  width: 100%;
  min-width: 0;
  min-height: 48px;
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-bg);
  color: var(--color-text);
  font: inherit;
  font-weight: var(--font-normal);
}
.totp-new-output { margin-top: var(--space-6); padding: var(--space-5); border: 1px solid var(--color-border); border-radius: var(--radius-lg); background: var(--color-bg-secondary); }
.totp-new-output__key { display: grid; gap: var(--space-2); }
.totp-new-output__key strong { font-size: var(--text-sm); }
.totp-new-output__key code { display: block; padding: var(--space-3); border: 1px solid var(--color-border); border-radius: var(--radius-md); background: var(--color-bg); color: var(--color-text); font-family: var(--font-mono); font-size: var(--text-sm); font-weight: var(--font-semibold); letter-spacing: .08em; overflow-wrap: anywhere; user-select: all; }
.totp-new-output__actions { display: flex; flex-wrap: wrap; gap: var(--space-2); margin: var(--space-3) 0 var(--space-4); }
.totp-new-output__uri input { font-family: var(--font-mono); font-size: var(--text-xs); }
.totp-new-output__uri small { color: var(--color-text-secondary); font-size: var(--text-xs); font-weight: var(--font-normal); line-height: 1.5; }
#new-qr { display: block; width: min(240px, 100%); height: auto; margin: 0 auto var(--space-4); border: 8px solid #fff; border-radius: var(--radius-md); background: #fff; image-rendering: pixelated; }
.totp-new-output p { margin: 0 0 var(--space-4); color: var(--color-text-secondary); font-size: var(--text-sm); line-height: 1.6; }

.totp-secret-field { margin-bottom: var(--space-5); }
.totp-secret-field > label { font-size: var(--text-base); }
.totp-secret-field small { line-height: var(--leading-normal); }
.totp-field input[aria-invalid="true"] { border-color: var(--color-error); }

.totp-field input::placeholder { color: var(--color-text-muted); opacity: 1; }
.totp-field input:focus-visible,
.totp-field select:focus-visible,
.totp-label-options summary:focus-visible,
.totp-page button:focus-visible,
.totp-page a:focus-visible { outline: 3px solid var(--totp-focus); outline-offset: 2px; }
.totp-secret-input { display: flex; min-width: 0; }
.totp-secret-input input { flex: 1 1 auto; min-width: 0; border-radius: var(--radius-md) 0 0 var(--radius-md); }

.totp-visibility-button {
  flex: 0 0 auto;
  min-width: 64px;
  min-height: 48px;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-border);
  border-left: 0;
  border-radius: 0;
  background: var(--color-bg-secondary);
  color: var(--color-text);
  font: inherit;
  font-weight: var(--font-semibold);
}

.totp-visibility-button,
.totp-paste-button,
.totp-primary-button,
.totp-secondary-button,
.totp-quiet-button { cursor: pointer; }

.totp-field small { color: var(--color-text-secondary); font-size: var(--text-xs); font-weight: var(--font-normal); }

.totp-primary-button,
.totp-secondary-button,
.totp-quiet-button {
  min-height: 46px;
  padding: var(--space-3) var(--space-4);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  font: inherit;
  font-weight: var(--font-semibold);
  transition: background-color var(--transition-fast), border-color var(--transition-fast), color var(--transition-fast);
}

.totp-primary-button {
  width: 100%;
  margin-top: 0;
  background: var(--color-primary);
  color: var(--color-text-inverse);
}

.totp-primary-button:hover:not(:disabled) { background: var(--color-primary-hover); }
.totp-primary-button:disabled,
.totp-secondary-button:disabled { cursor: not-allowed; opacity: .62; }
.totp-form-status { min-height: 1.5em; margin: var(--space-3) 0 0; color: var(--color-text-secondary); font-size: var(--text-sm); }
.totp-form-status[data-state="error"] { color: var(--color-error); }
.totp-form-guidance { margin: var(--space-2) 0 0; color: var(--color-text-secondary); font-size: var(--text-xs); line-height: 1.5; }

.totp-code-result {
  padding: var(--space-5);
  background: var(--color-primary-lighter);
  text-align: center;
}
.totp-code-result[hidden] { display: none; }
.totp-code-result:focus { outline: 2px solid var(--totp-focus); outline-offset: 2px; }

.totp-code-topline { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); text-align: left; }
.totp-code-topline h3 { margin: 0; color: var(--color-text); font-size: var(--text-base); }
.totp-code-topline p { margin: var(--space-1) 0 0; color: var(--color-text-secondary); font-size: var(--text-sm); overflow-wrap: anywhere; }
.totp-code-topline p:empty { display: none; }
.totp-countdown { display: flex; align-items: baseline; gap: var(--space-1); white-space: nowrap; color: var(--color-text-secondary); font-size: var(--text-xs); }
.totp-countdown #countdown-value { color: var(--color-primary); font-size: var(--text-xl); font-weight: var(--font-bold); font-variant-numeric: tabular-nums; }
.totp-code-result[data-expiring="true"] .totp-countdown #countdown-value { color: var(--color-warning); }
.totp-code-value { display: block; margin: var(--space-5) 0 var(--space-4); color: var(--color-text); font-size: clamp(2.25rem, 8vw, 3rem); font-weight: var(--font-bold); letter-spacing: .12em; line-height: 1.15; font-variant-numeric: tabular-nums; user-select: all; }
.totp-cycle-progress { display: block; width: 100%; height: 6px; margin: 0 0 var(--space-5); border: 0; border-radius: var(--radius-full); overflow: hidden; accent-color: var(--color-primary); }
.totp-cycle-progress::-webkit-progress-bar { background: var(--color-border); border-radius: var(--radius-full); }
.totp-cycle-progress::-webkit-progress-value { background: var(--color-primary); border-radius: var(--radius-full); }
.totp-cycle-progress::-moz-progress-bar { background: var(--color-primary); border-radius: var(--radius-full); }
.totp-code-result[data-expiring="true"] .totp-cycle-progress { accent-color: var(--color-warning); }
.totp-code-result[data-expiring="true"] .totp-cycle-progress::-webkit-progress-value { background: var(--color-warning); }
.totp-code-result[data-expiring="true"] .totp-cycle-progress::-moz-progress-bar { background: var(--color-warning); }
.totp-result-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--space-2); }
.totp-save-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: end; gap: var(--space-3); margin-top: var(--space-5); padding-top: var(--space-5); border-top: 1px solid var(--color-border); text-align: left; }
.totp-save-row .totp-field { margin: 0; }
.totp-save-row > button { min-height: 48px; }
.totp-save-row + .totp-form-status { text-align: left; }
.totp-secondary-button { border-color: var(--color-primary); background: var(--color-primary-light); color: var(--color-primary); }
.totp-secondary-button:hover:not(:disabled) { border-color: var(--color-primary-hover); }
.totp-quiet-button { border-color: var(--color-border); background: var(--color-bg); color: var(--color-text); }
.totp-quiet-button:hover { background: var(--color-bg-tertiary); }

.totp-vault { padding: var(--space-4); border-top: 1px solid var(--color-border); background: var(--color-bg); }
.totp-vault-heading { margin-bottom: var(--space-3); }
.totp-vault-heading > div { display: flex; align-items: baseline; flex-wrap: wrap; gap: 4px var(--space-2); }
.totp-vault-heading__kicker { color: var(--color-primary); font-size: 10px; font-weight: var(--font-bold); letter-spacing: .02em; white-space: nowrap; }
.totp-vault-heading h2 { margin: 0; color: var(--color-text); font-size: var(--text-base); line-height: 1.35; }
.totp-vault-heading > .totp-vault-heading__action { display: flex; flex-wrap: nowrap; align-items: center; justify-content: space-between; gap: var(--space-2); margin-top: var(--space-1); }
.totp-vault-heading p { margin: 0; color: var(--color-text-secondary); font-size: var(--text-xs); line-height: 1.5; }
.totp-vault-toggle { flex: 0 0 auto; min-height: 40px; padding: var(--space-2) var(--space-3); border: 1px solid var(--color-primary); border-radius: var(--radius-md); background: var(--color-primary-light); color: var(--color-primary); font: inherit; font-size: var(--text-xs); font-weight: var(--font-semibold); cursor: pointer; }
.totp-vault-toggle:hover { border-color: var(--color-primary-hover); }
.totp-vault-locked-hint { margin: 0 0 var(--space-3); color: var(--color-text-secondary); font-size: var(--text-xs); line-height: 1.5; }
.totp-vault-recovery { margin-top: var(--space-3); color: var(--color-text-secondary); font-size: var(--text-xs); }
.totp-vault-recovery summary { cursor: pointer; }
.totp-vault-recovery p { margin: var(--space-2) 0; line-height: 1.5; }
.totp-vault-recovery__actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.totp-vault-recovery__actions button { min-height: 40px; padding: var(--space-2) var(--space-3); font-size: var(--text-xs); }
.totp-paste-button { flex: 0 0 auto; min-height: 48px; padding: var(--space-2); border: 1px solid var(--color-border); border-left: 0; border-radius: 0 var(--radius-md) var(--radius-md) 0; background: var(--color-primary-light); color: var(--color-primary); font: inherit; font-size: var(--text-xs); font-weight: var(--font-semibold); }
.totp-paste-button:hover { background: var(--color-primary-lighter); }
#vault-unlock-form { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: end; gap: var(--space-3); }
#vault-unlock-form .totp-field { margin: 0; }
#vault-unlock-button { width: auto; min-height: 48px; }
.totp-vault-toolbar { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); margin-bottom: var(--space-4); color: var(--color-text-secondary); font-size: var(--text-sm); }
.totp-vault-list { display: grid; gap: var(--space-3); margin: 0; padding: 0; list-style: none; }
.totp-vault-item { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: var(--space-2); align-items: center; padding: var(--space-4); border: 1px solid var(--color-border); border-radius: var(--radius-lg); background: var(--color-bg-secondary); }
.totp-vault-item__name { margin: 0; color: var(--color-text); font-size: var(--text-base); font-weight: var(--font-semibold); overflow-wrap: anywhere; }
.totp-vault-item__meta { margin: var(--space-1) 0 0; color: var(--color-text-secondary); font-size: var(--text-xs); }
.totp-vault-item__code { color: var(--color-primary); font-family: var(--font-mono); font-size: var(--text-xl); font-weight: var(--font-bold); letter-spacing: .05em; font-variant-numeric: tabular-nums; text-align: right; }
.totp-vault-item__actions { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: var(--space-2); }
.totp-vault-item__actions button { min-height: 40px; padding: var(--space-2) var(--space-3); }
.totp-vault-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); margin-top: var(--space-5); }
.totp-vault-import { margin: var(--space-4) 0 0; }
.totp-vault-import__controls { display: flex; flex-wrap: wrap; align-items: end; gap: var(--space-3); padding: 0 var(--space-4); }
.totp-vault-import__controls .totp-field { flex: 1 1 250px; margin: 0; }
.totp-vault-import input[type="file"] { height: auto; padding: var(--space-2); font-size: var(--text-xs); }
.totp-vault-import__controls > button { min-height: 48px; }
.totp-vault-import p { margin: 0; padding: var(--space-2) var(--space-4) var(--space-4); color: var(--color-text-secondary); font-size: var(--text-xs); }
.totp-danger-button { color: var(--color-error); }
.totp-danger-button:hover { color: var(--color-error); border-color: var(--color-error); }
#vault-empty { margin: 0; color: var(--color-text-secondary); font-size: var(--text-sm); }
.totp-vault-note { margin: var(--space-4) 0 0; padding-top: var(--space-4); border-top: 1px solid var(--color-border); color: var(--color-text-secondary); font-size: var(--text-xs); line-height: 1.6; }
.totp-vault #vault-status:empty { display: none; }

[hidden] { display: none !important; }

@container (max-width: 820px) {
  .totp-workspace { grid-template-columns: 1fr; }
  .totp-code-empty { min-height: 160px; }
}

@media (max-width: 720px) {
  .totp-form-grid { grid-template-columns: 1fr; gap: 0; }
  .totp-local-badge { white-space: normal; }
  .totp-save-row { grid-template-columns: 1fr; }
  .totp-save-row > button { width: 100%; }
  .totp-vault-item { grid-template-columns: 1fr; }
  .totp-vault-item__code { text-align: left; }
  #vault-unlock-form { grid-template-columns: 1fr; }
  #vault-unlock-button, .totp-vault-import__controls > button { width: 100%; }
}

@media (max-width: 480px) {
  body.totp-page { min-width: 0; }
  .totp-code-topline { flex-direction: column; align-items: flex-start; gap: var(--space-2); }
  .totp-code-value { letter-spacing: .08em; }
  .totp-result-actions > button { flex: 1 1 auto; }
  .totp-label-options .totp-form-grid { gap: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .totp-page *, .totp-page *::before, .totp-page *::after { transition-duration: 0.01ms; }
}
