:root {
  color-scheme: light;
  --bg: #f4f6f1;
  --surface: #ffffff;
  --surface-2: #eef3e9;
  --ink: #18211c;
  --muted: #68756d;
  --line: #d8dfd4;
  --green: #29754d;
  --green-dark: #18563a;
  --teal: #0f6f74;
  --amber: #b56a17;
  --red: #b43b38;
  --shadow: 0 12px 30px rgba(24, 33, 28, 0.08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  min-width: 320px;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
  border-radius: 7px;
  background: var(--green);
  color: #fff;
  padding: 0.7rem 0.9rem;
  cursor: pointer;
  font-weight: 700;
  min-height: 40px;
}

button:hover {
  background: var(--green-dark);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

button.secondary {
  background: #e2eadd;
  color: var(--ink);
}

button.secondary:hover {
  background: #d3dfcb;
}

button.warn {
  background: var(--amber);
}

button.danger {
  background: var(--red);
}

button.ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0.68rem 0.75rem;
  background: #fff;
  color: var(--ink);
}

textarea {
  min-height: 130px;
  resize: vertical;
}

label {
  display: grid;
  gap: 0.35rem;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.2rem;
}

.login-panel {
  width: min(440px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 1.4rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  margin-bottom: 1.2rem;
}

.brand-logo {
  display: block;
  width: 150px;
  max-width: 100%;
  height: auto;
  background: #fff;
  border-radius: 6px;
  padding: 0.2rem;
}

.brand h1,
.brand h2 {
  margin: 0;
  font-size: 1.15rem;
}

.brand p {
  margin: 0.15rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.form-stack {
  display: grid;
  gap: 0.85rem;
}

.hint {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.45;
}

.error {
  color: var(--red);
  font-weight: 700;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr);
}

.sidebar {
  background: #16241d;
  color: #f7fbf5;
  padding: 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.sidebar .brand {
  align-items: flex-start;
  flex-direction: column;
  margin-bottom: 0.4rem;
}

.sidebar .brand p {
  color: #b8c8bd;
}

.login-logo {
  width: 220px;
}

.nav {
  display: grid;
  gap: 0.35rem;
}

.nav button {
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-align: left;
  background: transparent;
  color: #edf6ef;
  border: 1px solid transparent;
  min-height: 42px;
}

.nav button:hover,
.nav button.active {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.16);
}

.sidebar-footer {
  margin-top: auto;
  display: grid;
  gap: 0.75rem;
  color: #b8c8bd;
  font-size: 0.9rem;
}

.main {
  min-width: 0;
  padding: 1.1rem;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
}

.topbar h1 {
  margin: 0;
  font-size: 1.45rem;
}

.topbar p {
  margin: 0.2rem 0 0;
  color: var(--muted);
}

.grid {
  display: grid;
  gap: 1rem;
}

.grid.two {
  grid-template-columns: minmax(0, 1fr) minmax(340px, 0.72fr);
}

.grid.three {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 1rem;
  min-width: 0;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.9rem;
}

.panel-header h2,
.panel h3 {
  margin: 0;
  font-size: 1rem;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1rem;
}

.stat strong {
  display: block;
  font-size: 1.7rem;
  margin-bottom: 0.25rem;
}

.stat span {
  color: var(--muted);
  font-weight: 700;
}

.toolbar {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  align-items: end;
}

.toolbar > * {
  flex: 1 1 180px;
}

.toolbar .compact {
  flex: 0 0 auto;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
  background: #fff;
}

th,
td {
  text-align: left;
  padding: 0.75rem;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}

th {
  background: var(--surface-2);
  font-size: 0.82rem;
  text-transform: uppercase;
  color: #526157;
}

tr:last-child td {
  border-bottom: 0;
}

tr.active-row td {
  background: #f2f7ee;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  border-radius: 999px;
  padding: 0.25rem 0.55rem;
  background: #eaf2e5;
  color: var(--green-dark);
  font-size: 0.8rem;
  font-weight: 800;
  white-space: nowrap;
}

.badge.teal {
  background: #e0f0f0;
  color: var(--teal);
}

.badge.amber {
  background: #f7ead8;
  color: #7e4810;
}

.badge.red {
  background: #fae3e1;
  color: var(--red);
}

.split-editor {
  display: grid;
  grid-template-columns: minmax(280px, 0.78fr) minmax(320px, 1fr);
  gap: 1rem;
}

.list {
  display: grid;
  gap: 0.55rem;
}

.list-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 0.75rem;
  cursor: pointer;
}

.list-item:hover,
.list-item.active {
  border-color: var(--green);
  box-shadow: 0 0 0 2px rgba(41, 117, 77, 0.12);
}

.list-item strong {
  display: block;
  margin-bottom: 0.25rem;
}

.list-item p {
  margin: 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.mail-body {
  white-space: pre-wrap;
  background: #f9faf8;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.8rem;
  max-height: 240px;
  overflow: auto;
}

.actions {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-top: 0.8rem;
}

.chat-layout {
  display: grid;
  grid-template-rows: minmax(340px, 1fr) auto;
  gap: 0.8rem;
}

.chat-stream {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 0.8rem;
  display: grid;
  align-content: start;
  gap: 0.7rem;
}

.message {
  max-width: 760px;
  border-left: 4px solid var(--green);
  background: #f9fbf8;
  padding: 0.65rem 0.75rem;
  border-radius: 0 8px 8px 0;
}

.message-meta {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  margin-bottom: 0.25rem;
}

.message p {
  margin: 0;
  white-space: pre-wrap;
}

.message-delete {
  margin-top: 0.55rem;
}

.role-grid,
.permission-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0.5rem;
}

.check-row {
  display: flex;
  align-items: flex-start;
  gap: 0.45rem;
  padding: 0.45rem;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
}

.check-row input {
  width: auto;
  margin-top: 0.18rem;
}

.check-row span {
  color: var(--ink);
  font-weight: 800;
}

.check-row small {
  color: var(--muted);
  display: block;
}

.empty {
  color: var(--muted);
  padding: 1rem;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: #fbfcfa;
}

button.compact {
  padding: 0.4rem 0.7rem;
  min-height: 32px;
  font-size: 0.87rem;
}

/* --- Mail Center --- */

.mail-center {
  display: flex;
  flex-direction: column;
}

.sub-tabs {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--line);
  margin-bottom: 1rem;
}

.sub-tabs button {
  background: transparent;
  color: var(--muted);
  border: none;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  padding: 0.55rem 1rem;
  font-weight: 700;
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  margin-bottom: -2px;
}

.sub-tabs button:hover {
  color: var(--ink);
  background: var(--surface-2);
}

.sub-tabs button.active {
  color: var(--green);
  border-bottom-color: var(--green);
  background: transparent;
}

.mail-split {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  gap: 1rem;
  min-height: 520px;
}

.mail-list-panel {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.75rem;
}

.mail-list-scroll {
  flex: 1;
  overflow-y: auto;
  display: grid;
  gap: 0.35rem;
  align-content: start;
  max-height: 560px;
}

.mail-list-item {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  padding: 0.6rem 0.7rem;
  cursor: pointer;
  transition: border-color 0.12s, background 0.1s;
}

.mail-list-item:hover {
  border-color: var(--green);
}

.mail-list-item.active {
  border-color: var(--green);
  background: #f2f9ee;
  box-shadow: 0 0 0 2px rgba(41, 117, 77, 0.1);
}

.mail-list-item strong {
  display: block;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mail-list-item.unread strong {
  font-weight: 900;
}

.mail-from {
  margin: 0.15rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mail-date {
  font-size: 0.76rem;
  color: var(--muted);
}

.mail-detail-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  max-height: 580px;
}

.mail-meta {
  border-bottom: 1px solid var(--line);
  padding-bottom: 0.75rem;
}

.mail-meta h3 {
  margin: 0 0 0.5rem;
  font-size: 1.05rem;
}

.mail-meta-row {
  display: flex;
  gap: 0.5rem;
  font-size: 0.86rem;
  margin-bottom: 0.25rem;
  align-items: center;
  flex-wrap: wrap;
}

.mail-meta-row strong {
  min-width: 72px;
  color: var(--muted);
  font-weight: 700;
}

.draft-section {
  border-top: 2px solid var(--line);
  padding-top: 0.85rem;
}

.draft-section h4 {
  margin: 0 0 0.75rem;
  font-size: 0.95rem;
  color: var(--green-dark);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

@media (max-width: 980px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
  }

  .nav {
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  }

  .grid.two,
  .grid.three,
  .split-editor {
    grid-template-columns: 1fr;
  }

  .mail-split {
    grid-template-columns: 1fr;
  }

  .mail-list-scroll,
  .mail-detail-panel {
    max-height: 320px;
  }
}

@media (max-width: 620px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .main,
  .sidebar {
    padding: 0.8rem;
  }

  button {
    width: 100%;
  }

  .actions button,
  .toolbar button {
    width: auto;
  }
}
