:root {
  --bg: #0c1110;
  --bg-elevated: #151b18;
  --bg-soft: #1c2420;
  --border: #2c3832;
  --text: #eaf0eb;
  --muted: #8d9c93;
  --accent: #e08a3c;
  --accent-hover: #f0a05a;
  --accent-soft: rgba(224, 138, 60, 0.14);
  --danger: #e25b5b;
  --danger-soft: rgba(226, 91, 91, 0.14);
  --success: #3dbf7a;
  --info: #4ea3d9;
  --warn: #e6c35c;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.38);
  --font: "Outfit", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
  --sidebar-w: 232px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font);
  background:
    radial-gradient(1100px 520px at 8% -8%, rgba(224, 138, 60, 0.13), transparent 55%),
    radial-gradient(800px 420px at 100% 0%, rgba(61, 191, 122, 0.07), transparent 50%),
    var(--bg);
  color: var(--text);
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

a { color: inherit; }

.app-body {
  height: 100%;
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  height: 100vh;
  padding: 22px 16px;
  background: linear-gradient(180deg, #121815 0%, #0e1311 100%);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 8px;
}

.brand-logo {
  width: 40px;
  height: 40px;
  object-fit: contain;
  margin: 0;
}

.brand-name {
  font-weight: 700;
  letter-spacing: 0.02em;
}

.brand-sub {
  color: var(--muted);
  font-size: 0.75rem;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.nav-btn {
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  padding: 11px 13px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: color 0.18s ease, background 0.18s ease, border-color 0.18s ease, transform 0.15s ease;
}

.nav-btn:hover {
  color: var(--text);
  background: var(--bg-soft);
  transform: translateX(2px);
}

.nav-btn.active {
  color: var(--text);
  background: var(--accent-soft);
  border-color: rgba(224, 138, 60, 0.35);
}

.logout-link {
  margin-top: auto;
  text-decoration: none;
  text-align: center;
  padding: 11px;
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: #ffb4b4;
  border: 1px solid rgba(226, 91, 91, 0.3);
  transition: background 0.18s ease, transform 0.15s ease;
}

.logout-link:hover {
  background: rgba(226, 91, 91, 0.24);
  transform: translateY(-1px);
}

.main {
  min-width: 0;
  min-height: 0;
  height: 100vh;
  padding: 22px 24px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.panel-section {
  display: none;
  flex: 1;
  min-height: 0;
  flex-direction: column;
  animation: fadeIn 0.22s ease;
}

.panel-section.active,
.tabcontent.active {
  display: flex;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  margin-bottom: 14px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.panel-header-compact {
  margin-bottom: 10px;
}

.panel-header h1,
.panel-header h2,
.modal-header h2 {
  margin: 0 0 4px;
  font-size: 1.45rem;
  font-weight: 650;
}

.panel-desc,
.hint,
.muted {
  color: var(--muted);
}

.panel-desc {
  margin: 0;
  font-size: 0.9rem;
}

.filters-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.field-label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--muted);
}

.input {
  width: 100%;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.input:focus {
  border-color: rgba(224, 138, 60, 0.65);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.input:disabled,
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.input-inline {
  width: auto;
  min-width: 110px;
  padding: 7px 10px;
}

.table-card,
.create-form,
.log-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.fill-card {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.table-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

.data-table th,
.data-table td {
  padding: 11px 13px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.data-table thead th,
.sticky-head thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #1a221e;
  color: var(--muted);
  font-weight: 500;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.data-table tbody tr {
  transition: background 0.15s ease;
}

.data-table tbody tr:hover {
  background: rgba(255, 255, 255, 0.03);
}

.sortable { cursor: pointer; }
.sortable:hover { color: var(--text); }

.mono {
  font-family: var(--mono);
  font-size: 0.88rem;
}

.badge {
  display: inline-flex;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(61, 191, 122, 0.12);
  color: #8be0b0;
  font-size: 0.76rem;
  font-weight: 600;
}

.create-form {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  padding: 14px;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.toolbar {
  margin-bottom: 12px;
  flex-shrink: 0;
}

.search-user {
  max-width: 420px;
}

.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 15px;
  cursor: pointer;
  font-weight: 600;
  transition: background 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn:hover:not(:disabled) {
  transform: translateY(-1px);
}

.btn:active:not(:disabled) {
  transform: translateY(0);
}

.btn-primary {
  background: var(--accent);
  color: #1a120a;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-ghost:hover {
  background: var(--bg-soft);
}

.btn-sm {
  padding: 6px 11px;
  font-size: 0.84rem;
}

.btn-edit {
  background: rgba(78, 163, 217, 0.16);
  color: #9ed0ef;
  border: 1px solid rgba(78, 163, 217, 0.3);
}

.btn-edit:hover {
  background: rgba(78, 163, 217, 0.28);
}

.btn-delete {
  background: var(--danger-soft);
  color: #ffb4b4;
  border: 1px solid rgba(226, 91, 91, 0.3);
}

.btn-delete:hover {
  background: rgba(226, 91, 91, 0.24);
}

.btn-apply {
  background: rgba(61, 191, 122, 0.16);
  color: #8be0b0;
  border: 1px solid rgba(61, 191, 122, 0.35);
}

.btn-apply:hover:not(:disabled) {
  background: rgba(61, 191, 122, 0.28);
}

.row-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* Logs: full remaining space, equal 2x2 */
.logs-section .logs-grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 12px;
}

.log-card {
  min-height: 0;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: hidden;
}

.log-card-title {
  flex-shrink: 0;
  font-size: 0.76rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.log-card-title.debug { color: var(--info); }
.log-card-title.info { color: var(--success); }
.log-card-title.request { color: var(--warn); }
.log-card-title.error { color: var(--danger); }

.log-window {
  flex: 1;
  min-height: 0;
  width: 100%;
  height: 100%;
  resize: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #0a0e0c;
  color: #c9d4cc;
  padding: 10px;
  font-family: var(--mono);
  font-size: 0.76rem;
  line-height: 1.45;
}

/* Nick sync */
.stats-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 12px;
  flex-shrink: 0;
  animation: fadeIn 0.25s ease;
}

.stat-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 0.85rem;
}

.stat-pill .stat-label {
  color: var(--muted);
  font-family: var(--font);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-pill.warn {
  border-color: rgba(230, 195, 92, 0.4);
  background: rgba(230, 195, 92, 0.08);
}

.stat-pill.ok {
  border-color: rgba(61, 191, 122, 0.35);
  background: rgba(61, 191, 122, 0.08);
}

.sync-empty {
  padding: 48px 20px;
  text-align: center;
}

.sync-table select.input {
  min-width: 160px;
}

.sync-table th:nth-child(1),
.sync-table td:nth-child(1) { width: 70px; }
.sync-table th:nth-child(5),
.sync-table td:nth-child(5) { width: 120px; }

/* Spinner */
.btn.is-loading .btn-label {
  opacity: 0.55;
}

.btn-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(0, 0, 0, 0.2);
  border-top-color: rgba(0, 0, 0, 0.7);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.btn-ghost .btn-spinner,
.btn-apply .btn-spinner,
.btn-edit .btn-spinner {
  border-color: rgba(255, 255, 255, 0.2);
  border-top-color: rgba(255, 255, 255, 0.85);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.row-flash {
  animation: rowFlash 0.7s ease;
}

@keyframes rowFlash {
  0% { background: rgba(61, 191, 122, 0.22); }
  100% { background: transparent; }
}

.modal[hidden] {
  display: none !important;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 20px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(6, 10, 8, 0.72);
  backdrop-filter: blur(4px);
  animation: fadeIn 0.18s ease;
}

.modal-content {
  position: relative;
  width: min(460px, 100%);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  z-index: 1;
  animation: modalIn 0.22s ease;
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(10px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.icon-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}

.modal-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.hint {
  margin: 0;
  font-size: 0.82rem;
  line-height: 1.4;
}

.input-with-action {
  display: flex;
  gap: 8px;
  align-items: center;
}

.input-with-action .input {
  flex: 1;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
}

.toast-root {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 1100;
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: min(380px, calc(100vw - 32px));
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: #1b231f;
  box-shadow: var(--shadow);
  animation: toastIn 0.2s ease;
}

.toast-error {
  border-color: rgba(226, 91, 91, 0.45);
  background: #241616;
}

.toast-success {
  border-color: rgba(61, 191, 122, 0.45);
  background: #14241b;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: none; }
}

/* login */
.login_page,
.do_login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  overflow: auto;
}

.login_page form,
.do_login form {
  width: min(380px, 100%);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
  animation: modalIn 0.28s ease;
}

.login_page img {
  display: block;
  margin: 0 auto 18px;
  max-width: 120px;
}

.login_page table {
  width: 100%;
  border-collapse: collapse;
}

.login_page td {
  padding: 6px 0;
}

.login_page td:first-child {
  width: 72px;
  color: var(--muted);
  font-size: 0.9rem;
}

.login_page input[type="text"],
.login_page input[type="password"] {
  width: 100%;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 11px 12px;
}

.login_page input[type="submit"],
.do_login input[type="submit"] {
  width: 100%;
  margin-top: 14px;
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px;
  background: var(--accent);
  color: #1a120a;
  font-weight: 700;
  cursor: pointer;
}

.login_page input[type="submit"]:hover,
.do_login input[type="submit"]:hover {
  background: var(--accent-hover);
}

.do_login form div {
  margin: 8px 0;
  text-align: center;
}

@media (max-width: 980px) {
  html, body {
    overflow: auto;
    height: auto;
  }

  .app-shell {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100vh;
  }

  .sidebar {
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .nav {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .logout-link { margin-top: 0; }

  .main {
    height: auto;
    overflow: visible;
  }

  .panel-section.active {
    min-height: 70vh;
  }

  .create-form {
    grid-template-columns: 1fr;
  }

  .logs-section .logs-grid {
    grid-template-columns: 1fr;
    grid-template-rows: none;
    min-height: 120vh;
  }
}
