:root {
  --bg: #0b1017;
  --bg-glow: radial-gradient(1200px 600px at 10% -10%, rgba(59, 130, 246, 0.18), transparent 55%),
             radial-gradient(900px 500px at 90% 0%, rgba(99, 102, 241, 0.12), transparent 50%),
             radial-gradient(700px 400px at 50% 100%, rgba(34, 197, 94, 0.06), transparent 45%);
  --card: rgba(22, 30, 46, 0.82);
  --card-solid: #161e2e;
  --border: rgba(148, 163, 184, 0.16);
  --border-strong: rgba(148, 163, 184, 0.28);
  --text: #e8eef8;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-soft: rgba(59, 130, 246, 0.18);
  --danger: #ef4444;
  --success: #22c55e;
  --warn: #f59e0b;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.35), 0 1px 0 rgba(255, 255, 255, 0.04) inset;
  --shadow-sm: 0 4px 18px rgba(0, 0, 0, 0.25);
  --radius: 14px;
  --radius-sm: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --touch: 44px;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  overflow-x: hidden;
  width: 100%;
  max-width: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Noto Sans SC", sans-serif;
  background: var(--bg);
  background-image: var(--bg-glow);
  background-attachment: fixed;
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  width: 100%;
  max-width: 100vw;
  padding-bottom: var(--safe-bottom);
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding: calc(0.85rem + var(--safe-top)) calc(1.25rem + var(--safe-right)) 0.85rem calc(1.25rem + var(--safe-left));
  border-bottom: 1px solid var(--border);
  background: rgba(16, 22, 34, 0.88);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  position: sticky;
  top: 0;
  z-index: 40;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

.topbar nav {
  display: flex;
  gap: 0.35rem;
  align-items: center;
  flex-wrap: wrap;
}

.topbar nav a,
.topbar nav .link-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch);
  padding: 0 0.85rem;
  border-radius: 999px;
  color: var(--text);
  background: transparent;
  text-decoration: none;
  font-size: 0.92rem;
}

.topbar nav a:hover,
.topbar nav .link-btn:hover {
  background: var(--primary-soft);
  text-decoration: none;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.25rem calc(1.25rem + var(--safe-right)) 2rem calc(1.25rem + var(--safe-left));
}
.container.narrow { max-width: 480px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.2rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  max-width: 100%;
  overflow: visible;
}

.card h2 {
  margin: 0 0 0.85rem;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  align-items: center;
  margin-bottom: 1rem;
  position: sticky;
  top: calc(3.2rem + var(--safe-top));
  z-index: 30;
  padding: 0.75rem;
  border-radius: var(--radius);
  background: rgba(16, 22, 34, 0.78);
  border: 1px solid var(--border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow-sm);
  overflow: visible;
}

#task-status {
  flex: 1 1 100%;
  min-width: 0;
}

@media (min-width: 720px) {
  #task-status { flex: 1 1 auto; text-align: right; }
}

button, .link-btn {
  background: linear-gradient(180deg, rgba(55, 70, 95, 0.95), rgba(40, 52, 72, 0.95));
  color: var(--text);
  border: 1px solid var(--border-strong);
  padding: 0.55rem 1rem;
  min-height: var(--touch);
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease, background 0.15s ease;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.06) inset;
}

button.primary {
  background: linear-gradient(180deg, #4f8ff7, #2f6fdf);
  border-color: rgba(147, 197, 253, 0.35);
  box-shadow: 0 8px 20px rgba(59, 130, 246, 0.28), 0 1px 0 rgba(255, 255, 255, 0.12) inset;
}

button:hover { opacity: 0.95; transform: translateY(-1px); }
button:active { transform: translateY(0); }
.link-btn {
  background: transparent;
  border: none;
  box-shadow: none;
  padding: 0;
  min-height: auto;
}
.topbar .link-btn { min-height: var(--touch); }

.table-wrap {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
  margin: 0;
  padding-bottom: 0.15rem;
}

table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { padding: 0.7rem 0.55rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { color: var(--muted); font-weight: 500; white-space: nowrap; }

.status-active { color: var(--success); }
.status-disabled { color: var(--warn); }
.status-renewing { color: var(--primary); }
.status-partial { color: var(--warn); }

tr.row-renewing { opacity: 0.85; }
button:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.auth-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: calc(1rem + var(--safe-top)) calc(1rem + var(--safe-right)) calc(1rem + var(--safe-bottom)) calc(1rem + var(--safe-left));
}

.auth-card {
  width: min(400px, 100%);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 4px);
  padding: 2rem 1.5rem;
  box-shadow: var(--shadow), 0 0 80px rgba(59, 130, 246, 0.12);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.auth-card h1 {
  margin: 0 0 0.35rem;
  font-size: 1.45rem;
  letter-spacing: -0.02em;
}

.auth-card button[type="submit"] {
  width: 100%;
  margin-top: 0.35rem;
  background: linear-gradient(180deg, #4f8ff7, #2f6fdf);
  border-color: rgba(147, 197, 253, 0.35);
  min-height: var(--touch);
}

label {
  display: block;
  margin-bottom: 0.9rem;
  font-size: 0.85rem;
  color: var(--muted);
}

input, select {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.7rem 0.75rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: rgba(11, 16, 23, 0.85);
  color: var(--text);
  font-size: 16px; /* prevent iOS zoom */
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input:focus, select:focus {
  outline: none;
  border-color: rgba(59, 130, 246, 0.65);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.18);
}

.checkbox { display: flex; align-items: center; gap: 0.5rem; color: var(--text); min-height: var(--touch); }
.checkbox input { width: auto; margin: 0; min-width: 20px; min-height: 20px; }

.muted { color: var(--muted); font-size: 0.85rem; }
.error { color: var(--danger); font-size: 0.85rem; }
.success { color: var(--success); font-size: 0.85rem; }
.hidden { display: none; }

.logs { font-size: 0.8rem; max-height: 420px; overflow: auto; -webkit-overflow-scrolling: touch; }
.log-item { padding: 0.7rem 0; border-bottom: 1px solid var(--border); }
.log-head { display: flex; flex-wrap: wrap; gap: 0.35rem; align-items: center; }
.log-summary {
  margin-top: 0.35rem;
  color: var(--muted);
  white-space: pre-wrap;
  word-break: break-word;
}
.log-summary.error { color: var(--danger); }
.log-detail { margin-top: 0.4rem; }
.log-detail summary {
  cursor: pointer;
  color: var(--primary);
  font-size: 0.78rem;
  min-height: var(--touch);
  display: inline-flex;
  align-items: center;
}
.log-detail pre {
  margin: 0.4rem 0 0;
  padding: 0.6rem;
  border-radius: 8px;
  background: rgba(11, 16, 23, 0.9);
  border: 1px solid var(--border);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 220px;
  overflow: auto;
  font-size: 0.72rem;
  line-height: 1.45;
}

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card-solid);
  color: var(--text);
  padding: 1.25rem;
  max-width: 480px;
  width: calc(100vw - 2rem);
  box-shadow: var(--shadow);
}
dialog::backdrop {
  background: rgba(5, 8, 14, 0.72);
  backdrop-filter: blur(4px);
}

.dialog-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  flex-wrap: wrap;
  margin-top: 1rem;
}
.dialog-actions button { min-height: var(--touch); }

.toggle {
  position: relative;
  width: 48px;
  height: 28px;
  min-height: 28px;
  background: var(--border-strong);
  border-radius: 999px;
  cursor: pointer;
  border: none;
  padding: 0;
  box-shadow: none;
}
.toggle.on { background: var(--primary); }
.toggle:hover { transform: none; opacity: 1; }
.toggle::after {
  content: "";
  position: absolute;
  top: 4px;
  left: 4px;
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 50%;
  transition: transform 0.18s ease;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
}
.toggle.on::after { transform: translateX(20px); }

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}
.row-actions button {
  padding: 0.45rem 0.75rem;
  font-size: 0.82rem;
  min-height: 40px;
  margin-right: 0;
}

tr.row-highlight {
  background: rgba(255, 193, 7, 0.12);
  outline: 1px solid rgba(255, 193, 7, 0.45);
}

.cell-input,
.cell-select {
  width: 100%;
  min-width: 4.5rem;
  max-width: 7.5rem;
  margin: 0;
  padding: 0.35rem 0.45rem;
  font-size: 16px;
  box-sizing: border-box;
}

.cell-select { max-width: 5.5rem; }
.cell-input-wide { min-width: 8rem; max-width: 12rem; }

.cell-input.invalid {
  border-color: var(--danger);
  box-shadow: 0 0 0 1px var(--danger);
}

/* Mobile: stacked domain cards */
@media (max-width: 1100px) {
  .topbar {
    padding-left: calc(0.9rem + var(--safe-left));
    padding-right: calc(0.9rem + var(--safe-right));
    flex-direction: column;
    align-items: stretch;
    gap: 0.2rem;
  }

  .topbar nav {
    width: 100%;
    justify-content: flex-start;
    gap: 0.15rem;
  }

  .topbar > div {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  .container {
    padding-left: calc(0.85rem + var(--safe-left));
    padding-right: calc(0.85rem + var(--safe-right));
    padding-top: 1rem;
  }

  /* Compact chip toolbar — avoid full-width giant buttons */
  .toolbar {
    position: static;
    top: auto;
    gap: 0.4rem;
    padding: 0.55rem 0.65rem;
    margin-top: 0.15rem;
    align-items: stretch;
  }

  .toolbar button {
    flex: 0 1 auto;
    justify-content: center;
    min-height: 34px;
    padding: 0.35rem 0.75rem;
    font-size: 0.8rem;
    font-weight: 500;
    border-radius: 999px;
    box-shadow: none;
    opacity: 1 !important;
    transform: none !important;
    visibility: visible !important;
  }

  .toolbar button.primary {
    flex: 0 1 auto;
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.22);
  }

  #task-status {
    flex: 1 1 100%;
    font-size: 0.75rem;
    line-height: 1.35;
  }

  .card { padding: 0.85rem; }

  /* Keep real table on mobile — horizontal scroll for management */
  .table-wrap {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    margin: 0 -0.25rem;
    padding: 0 0.25rem 0.35rem;
    border-radius: 8px;
  }

  #domains-table {
    display: table !important;
    width: max-content;
    min-width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 0.8rem;
  }

  #domains-table thead {
    display: table-header-group !important;
  }

  #domains-table tbody {
    display: table-row-group !important;
  }

  #domains-table tr {
    display: table-row !important;
    background: transparent;
    border: none;
    border-radius: 0;
    padding: 0;
    margin: 0;
    box-shadow: none;
  }

  #domains-table th,
  #domains-table td {
    display: table-cell !important;
    width: auto !important;
    max-width: none !important;
    box-sizing: border-box;
    padding: 0.45rem 0.4rem;
    white-space: nowrap;
    vertical-align: middle;
    border-bottom: 1px solid var(--border);
  }

  #domains-table td::before,
  #domains-table td:first-child::before {
    content: none !important;
  }

  #domains-table th:first-child,
  #domains-table td:first-child {
    position: sticky;
    left: 0;
    z-index: 2;
    background: var(--card-solid);
    box-shadow: 4px 0 8px rgba(0, 0, 0, 0.18);
  }

  #domains-table th:nth-child(2),
  #domains-table td:nth-child(2) {
    position: sticky;
    left: 2.4rem;
    z-index: 2;
    background: var(--card-solid);
    box-shadow: 4px 0 8px rgba(0, 0, 0, 0.12);
    max-width: 9.5rem;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  #domains-table thead th:first-child,
  #domains-table thead th:nth-child(2) {
    z-index: 3;
    background: #121a28;
  }

  #domains-table .cell-input,
  #domains-table .cell-select {
    min-width: 4.5rem;
    max-width: 6.5rem;
    padding: 0.3rem 0.4rem;
    font-size: 14px;
  }

  #domains-table .cell-input-wide {
    min-width: 7.5rem;
    max-width: 10rem;
    font-size: 14px;
  }

  #domains-table .row-actions {
    display: flex !important;
    flex-direction: row;
    flex-wrap: nowrap;
    gap: 0.3rem;
    align-items: center;
  }

  #domains-table .row-actions button {
    width: auto;
    min-height: 30px;
    padding: 0.25rem 0.55rem;
    font-size: 0.75rem;
    white-space: nowrap;
  }

  dialog {
    width: calc(100vw - 1rem);
    max-width: none;
    margin: auto;
    padding: 1rem;
    border-radius: 16px 16px 12px 12px;
  }

  .dialog-actions button { flex: 1 1 auto; }
}

@media (max-width: 480px) {
  .auth-card { padding: 1.5rem 1.1rem; }
  .toolbar {
    padding: 0.5rem 0.55rem;
    gap: 0.35rem;
  }
  .toolbar button {
    flex: 0 1 auto;
    min-height: 32px;
    padding: 0.3rem 0.65rem;
    font-size: 0.78rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  button:hover { transform: none; }
}
