/* === Design Tokens === */
:root {
  --accent: #0d9488;
  --accent-hover: #0f766e;
  --accent-soft: rgba(13, 148, 136, 0.1);

  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --danger-soft: rgba(220, 38, 38, 0.08);

  --radius: 10px;
  --radius-sm: 6px;
  --radius-lg: 16px;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.18);

  --transition: 150ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* Light Theme (default) */
:root {
  --bg: #f7f8fa;
  --bg-elev: #ffffff;
  --bg-hover: #f1f3f6;
  --border: #e4e7ec;
  --border-strong: #cdd2da;
  --text: #1a202c;
  --text-muted: #64748b;
  --text-subtle: #94a3b8;
  --backdrop: rgba(15, 23, 42, 0.4);
}

/* Dark Theme via System Preference */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --bg-elev: #1a2028;
    --bg-hover: #232b35;
    --border: #2a3340;
    --border-strong: #3a4555;
    --text: #e8eef5;
    --text-muted: #94a3b8;
    --text-subtle: #64748b;
    --backdrop: rgba(0, 0, 0, 0.6);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.5);

    --accent-soft: rgba(13, 148, 136, 0.18);
    --danger-soft: rgba(220, 38, 38, 0.15);
  }
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  overscroll-behavior-y: none;
}

body {
  display: flex;
  flex-direction: column;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
}

h1 { font-size: 24px; font-weight: 700; margin: 0; letter-spacing: -0.02em; }
h2 { font-size: 16px; font-weight: 600; margin: 0; }
h3 { font-size: 15px; font-weight: 600; margin: 0 0 8px; }

p { margin: 0; }
.muted { color: var(--text-muted); }
.small { font-size: 13px; }

/* === Topbar === */
.topbar {
  position: sticky;
  top: 0;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 12px 16px;
  gap: 12px;
  height: 52px;
  z-index: 50;
  backdrop-filter: saturate(180%) blur(10px);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
}

.topbar-back, .topbar-menu {
  background: transparent;
  border: none;
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition);
}

.topbar-back:hover, .topbar-menu:hover {
  background: var(--bg-hover);
}

.topbar-title {
  font-weight: 600;
  font-size: 16px;
  flex: 1;
  text-align: center;
}

/* === Layout === */
.page {
  max-width: 720px;
  margin: 0 auto;
  padding: 20px 16px 80px;
  width: 100%;
}

.page-center {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

/* === Cards === */
.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 14px;
  box-shadow: var(--shadow-sm);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.card.warning {
  border-color: var(--warning);
  background: linear-gradient(0deg, var(--bg-elev), var(--bg-elev)),
              rgba(217, 119, 6, 0.04);
}
.card.danger {
  border-color: var(--danger);
  background: linear-gradient(0deg, var(--bg-elev), var(--bg-elev)),
              var(--danger-soft);
}

/* === Buttons === */
.btn {
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all var(--transition);
  font-family: inherit;
}

.btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-strong);
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.btn-danger:hover {
  background: #b91c1c;
  border-color: #b91c1c;
}

.btn-block {
  width: 100%;
  justify-content: center;
}

.btn-small {
  padding: 6px 12px;
  font-size: 13px;
}

.btn-link {
  background: none;
  border: none;
  color: var(--accent);
  padding: 4px 0;
  cursor: pointer;
  font-size: 14px;
}

/* === Forms === */
.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-label {
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 500;
}

.field input[type="text"],
.field input[type="password"],
.field input[type="number"],
.field input[type="time"],
.field input[type="email"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  transition: border-color var(--transition);
  width: 100%;
}

.field input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
}

.checkbox-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}

.error {
  color: var(--danger);
  font-size: 13px;
  margin: 4px 0 0;
}

/* === Auth Page === */
.auth-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px 28px;
  max-width: 380px;
  width: 100%;
  box-shadow: var(--shadow-md);
  text-align: center;
}

.auth-card h1 { margin-bottom: 6px; }
.auth-card p { margin-bottom: 24px; }
.auth-card .form, .auth-card form {
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-logo {
  display: inline-flex;
  width: 64px;
  height: 64px;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: var(--radius-lg);
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
}

/* === Device Cards === */
.device-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: all var(--transition);
  box-shadow: var(--shadow-sm);
}

.device-card:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.device-card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.device-card-name { font-weight: 600; font-size: 16px; }
.device-card-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

.device-card-stats {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 10px;
}

.device-card-stats strong {
  color: var(--text);
  font-weight: 600;
}

/* === Progress === */
.progress-bar {
  height: 8px;
  background: var(--bg-hover);
  border-radius: 4px;
  overflow: hidden;
  margin: 14px 0;
}

.progress-fill {
  height: 100%;
  background: var(--success);
  transition: width 400ms ease, background-color 200ms ease;
  border-radius: 4px;
}
.progress-fill.warning { background: var(--warning); }
.progress-fill.danger  { background: var(--danger); }

/* === Usage Stats === */
.usage-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.usage-stat {
  background: var(--bg);
  padding: 14px 10px;
  border-radius: var(--radius-sm);
  text-align: center;
  border: 1px solid var(--border);
}

.usage-label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}

.usage-value {
  display: block;
  font-size: 20px;
  font-weight: 700;
  margin-top: 4px;
  letter-spacing: -0.01em;
}

.bonus-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 14px;
}

/* === Device Header === */
.device-header { margin-bottom: 18px; }
.device-header h1 { margin-bottom: 4px; }

/* === Token === */
.token-section {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.token {
  display: block;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  word-break: break-all;
  margin: 8px 0;
  user-select: all;
}

/* === App Rules List === */
.app-rule {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
.app-rule:last-child { border-bottom: none; }
.app-rule:first-child { padding-top: 0; }

.app-rule-info {
  flex: 1;
  min-width: 0;
}

.app-rule-name {
  font-weight: 500;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-rule-pkg {
  font-size: 11px;
  color: var(--text-muted);
  font-family: ui-monospace, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-rule-tag {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 12px;
  white-space: nowrap;
}
.app-rule-tag.blocked   { background: var(--danger-soft); color: var(--danger); }
.app-rule-tag.whitelist { background: rgba(22, 163, 74, 0.12); color: var(--success); }
.app-rule-tag.limit     { background: rgba(217, 119, 6, 0.12); color: var(--warning); }

/* === Liveness List === */
.liveness-list { margin-top: 12px; }
.liveness-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}
.liveness-item:last-child { border-bottom: none; }
.liveness-status {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 12px;
}
.liveness-status.pending   { background: rgba(217, 119, 6, 0.12); color: var(--warning); }
.liveness-status.confirmed { background: rgba(22, 163, 74, 0.12); color: var(--success); }
.liveness-status.expired   { background: var(--bg-hover); color: var(--text-muted); }

/* === Usage History === */
.usage-day {
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.usage-day:last-child { border-bottom: none; }
.usage-day:first-child { padding-top: 0; }

.usage-day-header {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
}

.usage-app {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-muted);
  padding: 2px 0;
}

/* === Empty State === */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}
.empty-icon {
  display: inline-flex;
  width: 72px;
  height: 72px;
  align-items: center;
  justify-content: center;
  background: var(--bg-hover);
  border-radius: 50%;
  color: var(--text-subtle);
  margin-bottom: 16px;
}
.empty-state h2 { color: var(--text); margin-bottom: 6px; }

/* === Badges === */
.badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent);
}

/* === Advanced (collapsible) === */
.advanced {
  margin-top: 24px;
}
.advanced summary {
  cursor: pointer;
  padding: 12px 0;
  font-weight: 500;
  font-size: 14px;
  color: var(--text-muted);
  user-select: none;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
.advanced summary::-webkit-details-marker { display: none; }
.advanced summary::before {
  content: "▸";
  display: inline-block;
  transition: transform var(--transition);
  color: var(--text-subtle);
}
.advanced[open] summary::before {
  transform: rotate(90deg);
}

/* === Modal === */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  animation: fade-in 200ms ease;
}

.modal {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  max-width: 440px;
  width: 100%;
  padding: 24px;
  box-shadow: var(--shadow-lg);
  animation: modal-pop 220ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal h2 { margin-bottom: 6px; }
.modal-desc { color: var(--text-muted); font-size: 14px; margin-bottom: 18px; }

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 20px;
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes modal-pop {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* === Toast === */
#toast-root {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  flex-direction: column-reverse;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--text);
  color: var(--bg-elev);
  padding: 10px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  box-shadow: var(--shadow-lg);
  animation: toast-in 200ms ease;
  pointer-events: auto;
}

.toast.error { background: var(--danger); color: #fff; }
.toast.success { background: var(--success); color: #fff; }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* === Spinner === */
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--bg-hover);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* === Mobile === */
@media (max-width: 600px) {
  .page { padding: 16px 12px 80px; }
  .usage-grid { grid-template-columns: 1fr; }
  .field-row { grid-template-columns: 1fr; }
  h1 { font-size: 22px; }
  .auth-card { padding: 24px 20px; }
}

/* PWA Standalone Anpassungen */
@media (display-mode: standalone) {
  body { padding-top: env(safe-area-inset-top); }
  .topbar { padding-top: max(12px, env(safe-area-inset-top)); height: auto; }
}
