:root {
  font-family: Inter, "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
  color: #20252c;
  background: #edf1f5;
  --bg: #edf1f5;
  --surface: #fff;
  --surface-2: #f6f8fa;
  --surface-3: #e9edf1;
  --border: #d5dbe1;
  --border-strong: #bcc5ce;
  --text: #20252c;
  --muted: #68727d;
  --primary: #d93f3f;
  --primary-hover: #be3030;
  --green: #16865a;
  --blue: #2476b8;
  --amber: #a86d09;
  --danger: #c83737;
  --shadow: 0 16px 44px rgba(30, 39, 48, .2);
}

:root[data-theme="dark"] {
  color: #e7eaee;
  background: #171a1e;
  --bg: #171a1e;
  --surface: #22262b;
  --surface-2: #292e34;
  --surface-3: #343a41;
  --border: #3a4149;
  --border-strong: #505963;
  --text: #e7eaee;
  --muted: #a3abb4;
  --shadow: 0 16px 44px rgba(0, 0, 0, .38);
}

* { box-sizing: border-box; }
html, body, #root { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body { background: var(--bg); color: var(--text); font-size: 13px; }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button { color: inherit; }
button:disabled { opacity: .45; cursor: not-allowed; }
input, select, textarea {
  width: 100%;
  min-height: 34px;
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text);
  background: var(--surface);
  padding: 7px 9px;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 2px color-mix(in srgb, var(--blue) 18%, transparent); }
textarea { min-height: 88px; resize: vertical; }
code, .mono, .log-view, .config-view, .code-input { font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace; }

.app-shell { display: grid; grid-template-rows: 46px 49px minmax(0, 1fr) 26px; width: 100%; height: 100%; background: var(--bg); }
.topbar { display: flex; align-items: center; border-bottom: 1px solid var(--border); background: var(--surface); min-width: 0; }
.brand { height: 100%; min-width: 220px; display: flex; align-items: center; gap: 9px; padding: 0 15px; border-right: 1px solid var(--border); }
.brand-mark, .brand-icon { color: var(--primary); font-weight: 900; }
.brand strong { font-size: 16px; }
.brand span { font-size: 9px; color: var(--muted); padding-left: 2px; }
.menu-strip { display: flex; align-items: stretch; height: 100%; }
.menu-strip button { min-width: 64px; border: 0; background: transparent; padding: 0 13px; cursor: pointer; }
.menu-strip button:hover { background: var(--surface-2); }
.top-actions { margin-left: auto; display: flex; padding: 0 9px; gap: 3px; }
.mobile-only { display: none !important; }

.toolbar { display: flex; align-items: center; gap: 9px; padding: 7px 10px; border-bottom: 1px solid var(--border); background: var(--surface-2); min-width: 0; overflow-x: auto; }
.toolbar-group, .core-controls { display: flex; align-items: center; gap: 5px; }
.toolbar-separator { height: 25px; width: 1px; background: var(--border); }
.toolbar-spacer { flex: 1; }
.icon-button, .command-button, .button {
  min-height: 32px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface);
  cursor: pointer;
}
.icon-button { min-width: 32px; padding: 0 8px; display: inline-grid; place-items: center; flex: 0 0 auto; }
.icon-button:hover, .command-button:hover, .button:hover { border-color: var(--border-strong); background: var(--surface-3); }
.icon-button.danger { color: var(--danger); }
.icon-button.success { color: var(--green); }
.icon-button.active { color: var(--primary); background: color-mix(in srgb, var(--primary) 9%, var(--surface)); border-color: color-mix(in srgb, var(--primary) 45%, var(--border)); }
.command-button { display: inline-flex; align-items: center; justify-content: center; gap: 7px; padding: 0 11px; white-space: nowrap; }
.command-button.primary { color: #fff; border-color: var(--primary); background: var(--primary); }
.command-button.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.command-button.full { width: 100%; }
.button { padding: 0 14px; }
.core-state { display: inline-flex; align-items: center; gap: 6px; margin-right: 4px; color: var(--muted); font-weight: 600; white-space: nowrap; }
.core-state i, .status-led { width: 8px; height: 8px; border-radius: 50%; background: var(--border-strong); display: inline-block; }
.core-state.running i, .status-led.online { background: var(--green); box-shadow: 0 0 0 3px color-mix(in srgb, var(--green) 15%, transparent); }
.core-state.unavailable i { background: var(--danger); }

.workspace { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 0; }
.sidebar { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); background: var(--surface); }
.sidebar-heading { min-height: 37px; display: flex; align-items: center; gap: 7px; padding: 0 11px; border-bottom: 1px solid var(--border); color: var(--muted); font-size: 11px; font-weight: 700; text-transform: uppercase; }
.sidebar-heading strong, .sidebar-heading button { margin-left: auto; }
.group-list, .sidebar-subscriptions { padding: 6px; }
.group-list button, .sidebar-subscriptions > button {
  width: 100%; min-height: 34px; display: flex; align-items: center; gap: 9px;
  border: 0; border-radius: 4px; padding: 0 8px; background: transparent; cursor: pointer; text-align: left;
}
.group-list button:hover, .sidebar-subscriptions > button:hover { background: var(--surface-2); }
.group-list button.active { color: var(--primary); background: color-mix(in srgb, var(--primary) 8%, var(--surface)); font-weight: 700; }
.group-list button span, .sidebar-subscriptions button span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group-list small, .sidebar-subscriptions small { color: var(--muted); }
.sidebar-subscriptions { border-top: 1px solid var(--border); margin-top: 4px; overflow-y: auto; }
.sidebar-subscriptions .sidebar-heading { margin: -6px -6px 4px; border-bottom: 0; }
.sidebar-core { margin-top: auto; padding: 11px; border-top: 1px solid var(--border); background: var(--surface-2); }
.sidebar-core div { display: flex; align-items: center; gap: 7px; }
.sidebar-core strong { margin-left: auto; font-size: 9px; color: var(--muted); }
.sidebar-core p { margin: 7px 0 0; color: var(--muted); font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.main-content { display: grid; grid-template-rows: 46px minmax(0, 1fr) auto; min-width: 0; min-height: 0; background: var(--surface); }
.list-header { display: flex; align-items: center; gap: 10px; padding: 6px 9px; border-bottom: 1px solid var(--border); }
.search-box { width: 260px; display: flex; align-items: center; gap: 7px; border: 1px solid var(--border); border-radius: 5px; padding: 0 8px; background: var(--surface); }
.search-box input { border: 0; box-shadow: none; min-height: 31px; padding: 5px 0; background: transparent; }
.search-box kbd { color: var(--muted); font-size: 10px; }
.selection-actions { flex: 1; min-width: 0; display: flex; align-items: center; justify-content: flex-end; gap: 5px; }
.selection-actions > span { margin-right: 6px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mobile-group-filter { display: none; }
.table-wrap { min-height: 0; overflow: auto; }
.profile-table { width: 100%; min-width: 1050px; border-collapse: separate; border-spacing: 0; }
.profile-table th {
  position: sticky; top: 0; z-index: 2; height: 34px; padding: 0 9px; border-bottom: 1px solid var(--border-strong);
  border-right: 1px solid var(--border); color: var(--muted); background: var(--surface-2); text-align: left; font-size: 11px; font-weight: 700;
}
.profile-table td { height: 42px; padding: 0 9px; border-bottom: 1px solid var(--border); border-right: 1px solid var(--border); white-space: nowrap; }
.profile-table tbody tr:hover td { background: var(--surface-2); }
.profile-table tbody tr.selected td { background: color-mix(in srgb, var(--blue) 8%, var(--surface)); }
.profile-table tbody tr.active-profile td:first-child { box-shadow: inset 3px 0 var(--primary); }
.check-cell { width: 35px; text-align: center !important; padding: 0 !important; }
.check-cell input { width: 15px; min-height: 15px; accent-color: var(--primary); }
.numeric { text-align: right !important; font-variant-numeric: tabular-nums; }
.name-cell { max-width: 260px; overflow: hidden; text-overflow: ellipsis; }
.active-flag { display: inline-block; margin-right: 7px; padding: 2px 5px; border-radius: 3px; color: #fff; background: var(--primary); font-size: 9px; font-weight: 700; }
.protocol { display: inline-block; min-width: 56px; padding: 3px 5px; border-radius: 3px; color: #fff; text-align: center; font-size: 10px; font-weight: 700; }
.protocol-vless { background: #317a5e; }.protocol-vmess { background: #376ea4; }.protocol-trojan { background: #7b4ea3; }
.protocol-shadowsocks { background: #b26a2f; }.protocol-socks { background: #596774; }.protocol-http { background: #25849b; }.protocol-custom { background: #74474b; }
.security-on { color: var(--green); font-weight: 700; }.muted { color: var(--muted); }
.delay.good { color: var(--green); font-weight: 700; }.delay.slow { color: var(--amber); }
.row-actions { display: flex; align-items: center; gap: 3px; }
.row-actions .icon-button { min-height: 27px; min-width: 27px; padding: 0 6px; border-color: transparent; background: transparent; }
.row-actions .icon-button:hover { border-color: var(--border); background: var(--surface-2); }

.bottom-panel { border-top: 1px solid var(--border); background: var(--surface); }
.bottom-tabs { height: 34px; display: grid; grid-template-columns: auto auto 1fr auto auto; align-items: center; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.bottom-tabs > button:not(.icon-button) { height: 34px; display: flex; align-items: center; gap: 6px; padding: 0 12px; border: 0; border-right: 1px solid var(--border); background: transparent; cursor: pointer; }
.bottom-tabs > button.active { color: var(--primary); background: var(--surface); box-shadow: inset 0 -2px var(--primary); }
.bottom-tabs .icon-button { border: 0; background: transparent; }
.log-view, .config-view { height: 170px; overflow: auto; margin: 0; padding: 7px 9px; color: #d7dce2; background: #171a1e; font-size: 11px; line-height: 1.55; }
.log-line { display: grid; grid-template-columns: 72px 52px minmax(0, 1fr); gap: 7px; }
.log-line time { color: #77818b; }.log-line > span { color: #86a8c3; text-transform: uppercase; }.log-line pre { margin: 0; white-space: pre-wrap; word-break: break-word; }
.log-error pre, .log-stderr pre { color: #ff9d9d; }.log-system pre { color: #8ed8b9; }
.log-empty { display: grid; place-items: center; height: 100%; color: #77818b; }
.config-view { color: #d6e3ed; white-space: pre-wrap; }

.statusbar { display: flex; align-items: center; gap: 16px; min-width: 0; padding: 0 9px; border-top: 1px solid var(--border); color: var(--muted); background: var(--surface-2); font-size: 10px; }
.statusbar > span { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
.status-spacer { flex: 1; }

.modal-backdrop { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; padding: 20px; background: rgba(15, 18, 22, .52); }
.modal { width: min(560px, 100%); max-height: min(820px, calc(100vh - 40px)); display: flex; flex-direction: column; overflow: hidden; border: 1px solid var(--border-strong); border-radius: 7px; background: var(--surface); box-shadow: var(--shadow); }
.modal-wide { width: min(960px, 100%); }
.modal-header { min-height: 66px; display: flex; align-items: center; padding: 10px 12px 10px 16px; border-bottom: 1px solid var(--border); }
.modal-header div:first-child { min-width: 0; }.modal-header h2 { margin: 0; font-size: 16px; }.modal-header p { margin: 4px 0 0; color: var(--muted); font-size: 11px; }
.modal-header .icon-button { margin-left: auto; border: 0; }
.modal-body { min-height: 0; overflow: auto; padding: 16px; }
.modal-footer { min-height: 56px; display: flex; align-items: center; justify-content: flex-end; gap: 8px; padding: 10px 16px; border-top: 1px solid var(--border); background: var(--surface-2); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 13px 16px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; color: var(--muted); font-size: 11px; font-weight: 600; }
.field-wide { grid-column: 1 / -1; }
.field small { color: var(--muted); font-weight: 400; }
.field input, .field select, .field textarea { color: var(--text); font-weight: 400; }
.code-input { min-height: 240px; line-height: 1.5; white-space: pre; }
.toggle-row { display: flex; align-items: center; gap: 9px; min-height: 34px; color: var(--text); font-size: 12px; cursor: pointer; }
.toggle-row input { width: 16px; min-height: 16px; accent-color: var(--primary); }
.form-error { padding: 9px 11px; border: 1px solid color-mix(in srgb, var(--danger) 45%, var(--border)); border-radius: 5px; color: var(--danger); background: color-mix(in srgb, var(--danger) 7%, var(--surface)); }
.section-title { grid-column: 1 / -1; margin: 8px 0 0; padding-top: 12px; border-top: 1px solid var(--border); font-weight: 800; color: var(--text); }
.subscription-layout { display: grid; grid-template-columns: 320px minmax(0, 1fr); gap: 18px; }
.subscription-form { align-self: start; display: grid; gap: 11px; padding: 14px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface-2); }
.subscription-list { display: flex; flex-direction: column; gap: 7px; min-width: 0; }
.subscription-item { min-height: 61px; display: flex; align-items: center; gap: 9px; padding: 8px 9px; border: 1px solid var(--border); border-radius: 5px; }
.subscription-main { min-width: 0; flex: 1; }
.subscription-main strong, .subscription-main p, .subscription-main small { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.subscription-main p { margin: 3px 0 0; color: var(--muted); font-size: 10px; }.subscription-main small { margin-top: 3px; color: var(--danger); }
.routing-toolbar { display: flex; align-items: center; gap: 14px; margin-bottom: 12px; }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 5px; overflow: hidden; }
.segmented button { min-height: 32px; border: 0; border-right: 1px solid var(--border); padding: 0 12px; background: var(--surface); cursor: pointer; }
.segmented button:last-child { border-right: 0; }.segmented button.active { color: #fff; background: var(--blue); }
.rule-list { display: flex; flex-direction: column; gap: 6px; }
.rule-row { display: grid; grid-template-columns: 130px minmax(150px, 1fr) minmax(150px, 1fr) 85px 90px 32px; gap: 6px; align-items: center; }
.backup-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.backup-action { min-height: 112px; display: flex; align-items: center; gap: 14px; border: 1px solid var(--border); border-radius: 6px; padding: 16px; color: var(--text); background: var(--surface-2); cursor: pointer; text-align: left; }
.backup-action:hover { border-color: var(--blue); }
.empty-state { min-height: 260px; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); }
.empty-state h3 { margin: 12px 0 3px; color: var(--text); }.empty-state p { margin: 0 0 14px; }

.login-page, .boot-screen { width: 100%; height: 100%; display: grid; place-items: center; background: var(--bg); }
.boot-screen { align-content: center; gap: 12px; color: var(--muted); }
.login-panel { width: min(430px, calc(100vw - 32px)); padding: 28px; border: 1px solid var(--border); border-radius: 7px; background: var(--surface); box-shadow: var(--shadow); }
.brand-mark { display: flex; align-items: center; gap: 9px; color: var(--primary); font-size: 18px; font-weight: 800; }
.login-panel h1 { margin: 26px 0 6px; font-size: 22px; }
.login-panel > p { margin: 0 0 20px; color: var(--muted); line-height: 1.5; }
.login-panel .field, .login-panel .form-error { margin-bottom: 13px; }

.toast-stack { position: fixed; z-index: 50; top: 58px; right: 14px; display: flex; flex-direction: column; gap: 7px; width: min(360px, calc(100vw - 28px)); }
.toast { min-height: 40px; display: flex; align-items: center; gap: 9px; padding: 9px 12px; border: 1px solid color-mix(in srgb, var(--green) 50%, var(--border)); border-radius: 5px; color: var(--text); background: var(--surface); box-shadow: 0 8px 24px rgba(20, 30, 40, .16); }
.toast.error { border-color: color-mix(in srgb, var(--danger) 50%, var(--border)); color: var(--danger); }

@media (max-width: 850px) {
  .app-shell { grid-template-rows: 46px 49px minmax(0, 1fr) 28px; }
  .brand { min-width: 170px; }
  .brand span { display: none; }
  .menu-strip { display: none; position: fixed; z-index: 20; top: 46px; right: 8px; height: auto; flex-direction: column; border: 1px solid var(--border); border-radius: 5px; background: var(--surface); box-shadow: var(--shadow); }
  .menu-strip.open { display: flex; }.menu-strip button { min-height: 38px; text-align: left; }
  .mobile-only { display: inline-grid !important; }
  .toolbar .toolbar-separator, .core-state { display: none; }
  .workspace { grid-template-columns: minmax(0, 1fr); }.sidebar { display: none; }
  .mobile-group-filter { display: flex; align-items: center; gap: 5px; }
  .mobile-group-filter select { min-width: 130px; }
  .search-box { width: min(230px, 45vw); }
  .selection-actions > span { display: none; }
  .profile-table { min-width: 960px; }
  .desktop-status { display: none !important; }
  .subscription-layout { grid-template-columns: 1fr; }
}

@media (max-width: 560px) {
  .brand { min-width: auto; border-right: 0; }
  .toolbar { gap: 5px; }
  .toolbar .command-button { font-size: 0; width: 32px; padding: 0; gap: 0; }
  .list-header { display: grid; grid-template-columns: 1fr 1fr; height: auto; min-height: 46px; }
  .search-box { width: 100%; }
  .selection-actions { grid-column: 1 / -1; justify-content: flex-start; }
  .main-content { grid-template-rows: auto minmax(0, 1fr) auto; }
  .bottom-panel.open .log-view, .bottom-panel.open .config-view { height: 145px; }
  .statusbar { gap: 10px; overflow: hidden; }
  .statusbar span:nth-child(n+4) { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  .field-wide { grid-column: auto; }
  .modal-backdrop { padding: 8px; }
  .modal { max-height: calc(100vh - 16px); }
  .routing-toolbar { align-items: stretch; flex-direction: column; }
  .segmented { display: grid; grid-template-columns: 1fr 1fr 1fr; }
  .rule-list { overflow-x: auto; }
  .rule-row { min-width: 760px; }
  .backup-actions { grid-template-columns: 1fr; }
}
