:root {
  --bg: #0f1115;
  --surface: #161923;
  --surface-2: #1e2230;
  --border: #2a2f3f;
  --text: #e8eaf0;
  --text-dim: #9aa1b4;
  --accent: #6c8cff;
  --accent-dim: #37407a;
  --green: #33c481;
  --yellow: #e0b23c;
  --red: #e5566e;
  --radius: 10px;
  --sidebar-w: 230px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
}

a { color: inherit; text-decoration: none; }

.app {
  display: flex;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 8px 20px 8px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}

.brand .logo {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), #a06cff);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  color: #fff;
}

.brand .name {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.3px;
}

.brand .sub {
  font-size: 11px;
  color: var(--text-dim);
}

.nav-section-label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-dim);
  margin: 18px 8px 6px 8px;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--text-dim);
  font-weight: 500;
  margin-bottom: 2px;
}

.nav-link .ic {
  width: 18px;
  text-align: center;
}

.nav-link:hover {
  background: var(--surface-2);
  color: var(--text);
}

.nav-link.active {
  background: var(--accent-dim);
  color: #fff;
}

/* Main */
.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 26px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: rgba(15, 17, 21, 0.85);
  backdrop-filter: blur(6px);
  z-index: 5;
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
}

.topbar .crumb {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 2px;
}

.content {
  padding: 24px 26px 60px 26px;
}

/* Stat tiles */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}

.stat-tile .label {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.stat-tile .value {
  font-size: 24px;
  font-weight: 700;
}

.stat-tile .delta {
  font-size: 12px;
  margin-top: 6px;
}

.delta.up { color: var(--green); }
.delta.down { color: var(--red); }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 20px;
}

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

.card-header h2 {
  font-size: 15px;
  margin: 0;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

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

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

.btn.primary:hover { filter: brightness(1.08); }

.btn.danger {
  background: transparent;
  border-color: var(--red);
  color: var(--red);
}

.btn.small {
  padding: 5px 10px;
  font-size: 12px;
}

/* Forms */
input, select, textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 10px;
  border-radius: 7px;
  font-size: 13px;
  font-family: inherit;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 12px;
}

.field label {
  font-size: 12px;
  color: var(--text-dim);
}

.search-box {
  min-width: 220px;
}

/* Table */
table {
  width: 100%;
  border-collapse: collapse;
}

thead th {
  text-align: left;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--text-dim);
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

tbody td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }

/* Badges */
.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.2px;
}

.badge.active, .badge.paid, .badge.completed { background: rgba(51,196,129,0.15); color: var(--green); }
.badge.pending, .badge.trial { background: rgba(224,178,60,0.15); color: var(--yellow); }
.badge.suspended, .badge.overdue, .badge.cancelled { background: rgba(229,86,110,0.15); color: var(--red); }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.modal-backdrop.open { display: flex; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 420px;
  max-width: calc(100vw - 40px);
  padding: 20px;
}

.modal h3 { margin-top: 0; }

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

/* Tree */
.tree {
  font-size: 13px;
}

.tree-node {
  margin-left: 18px;
}

.tree-node-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 7px;
}

.tree-node-row:hover { background: var(--surface-2); }

.tree-toggle {
  width: 16px;
  cursor: pointer;
  color: var(--text-dim);
  user-select: none;
}

.tree-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent-dim);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  color: #cdd6ff;
}

.tree-name { font-weight: 600; }
.tree-meta { color: var(--text-dim); font-size: 11.5px; }

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-dim);
}

.toast-wrap {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
}

.toast {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--green);
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}

.text-dim { color: var(--text-dim); }
.mono { font-variant-numeric: tabular-nums; }

/* Spreadsheet grid */
.sheet-wrap {
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 72vh;
}

table.sheet {
  border-collapse: separate;
  border-spacing: 0;
  background: #ffffff;
  color: #1a1a1a;
  font-size: 13px;
  width: max-content;
  min-width: 100%;
}

table.sheet th, table.sheet td {
  border-right: 1px solid #dde1e6;
  border-bottom: 1px solid #dde1e6;
  padding: 0;
}

table.sheet thead th {
  position: sticky;
  top: 0;
  z-index: 3;
}

table.sheet thead tr.col-letters th {
  background: #f3f4f6;
  color: #5f6368;
  font-weight: 600;
  text-align: center;
  font-size: 11px;
  padding: 4px 0;
  height: 20px;
}

table.sheet thead tr.col-headers th {
  top: 21px;
  background: #eef0f3;
  color: #1a1a1a;
  font-weight: 700;
  text-align: left;
  padding: 7px 10px;
  font-size: 12.5px;
  white-space: nowrap;
}

table.sheet th.corner {
  background: #f3f4f6;
  position: sticky;
  left: 0;
  z-index: 4;
  width: 36px;
}

table.sheet td.row-num {
  background: #f3f4f6;
  color: #5f6368;
  text-align: center;
  font-weight: 600;
  width: 36px;
  min-width: 36px;
  position: sticky;
  left: 0;
  z-index: 2;
  font-size: 11.5px;
  user-select: none;
}

table.sheet tbody tr:hover td.row-num {
  background: #e8eaed;
}

table.sheet td.cell {
  padding: 6px 10px;
  min-width: 130px;
  max-width: 260px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: cell;
  outline: none;
  position: relative;
}

table.sheet td.cell:hover {
  background: #f7f9fc;
}

table.sheet td.cell.selected {
  outline: 2px solid #1a73e8;
  outline-offset: -2px;
  background: #e8f0fe;
}

table.sheet td.cell input, table.sheet td.cell select {
  border: none;
  background: transparent;
  font-size: 13px;
  width: 100%;
  color: inherit;
  padding: 0;
  font-family: inherit;
}

table.sheet td.cell input:focus, table.sheet td.cell select:focus {
  outline: none;
}

table.sheet td.del-col {
  width: 32px;
  min-width: 32px;
  text-align: center;
  background: #fafbfc;
}

table.sheet td.del-col button {
  border: none;
  background: transparent;
  color: #c33;
  cursor: pointer;
  font-size: 14px;
  opacity: 0.5;
  padding: 4px 8px;
}

table.sheet td.del-col button:hover {
  opacity: 1;
}

table.sheet tbody tr:nth-child(even) td.cell {
  background: #fafbfc;
}

table.sheet tbody tr:nth-child(even) td.cell:hover {
  background: #f2f5fa;
}

table.sheet tbody tr:nth-child(even) td.cell.selected {
  background: #e8f0fe;
}
