:root {
  --font-sans: "Segoe UI Variable", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  --md-primary: #0b57d0;
  --md-primary-hover: #0842a0;
  --md-primary-container: #d3e3fd;
  --md-primary-surface: rgba(11, 87, 208, 0.08);
  --md-primary-surface-strong: rgba(11, 87, 208, 0.14);
  --md-focus-ring: rgba(11, 87, 208, 0.22);
  --md-secondary: #0b57d0;
  --md-error: #b3261e;
  --md-success: #137333;
  --md-surface: #ffffff;
  --md-surface-variant: #f1f3f4;
  --md-background: #f8fafd;
  --md-outline: #dadce0;
  --md-outline-strong: #c4c7c5;
  --md-text: #202124;
  --md-text-secondary: #5f6368;
  --md-on-primary: #ffffff;
  --md-brand-dark: #202124;
  --elevation-1: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);
  --elevation-2: 0 2px 6px rgba(60, 64, 67, 0.28), 0 1px 2px rgba(60, 64, 67, 0.18);
  --elevation-3: 0 4px 10px rgba(60, 64, 67, 0.24), 0 2px 4px rgba(60, 64, 67, 0.16);
  --radius-card: 8px;
  --radius-control: 6px;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
}

body {
  min-height: 100vh;
  margin: 0;
  font-family: var(--font-sans);
  color: var(--md-text);
  background: var(--md-background);
}

a {
  color: var(--md-primary);
}

a:hover {
  color: var(--md-primary-hover);
}

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

.btn:focus,
.btn:active:focus,
.btn-link.nav-link:focus,
.form-control:focus,
.form-check-input:focus,
input:focus,
select:focus,
textarea:focus,
.sidebar-link:focus-visible,
.sidebar-toggle:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--md-focus-ring);
}

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

.app-sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(32, 33, 36, 0.42);
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms cubic-bezier(0.2, 0, 0, 1);
}

.app-sidebar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  width: 304px;
  height: 100vh;
  max-height: 100vh;
  padding: 0 12px 16px;
  color: var(--md-text);
  background: var(--md-surface);
  border-right: 1px solid var(--md-outline);
  box-shadow: var(--elevation-1);
  overflow: hidden;
}

.sidebar-brand {
  margin: 0 -12px 14px;
  padding: 6px 0;
  background: var(--md-brand-dark);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: var(--elevation-1);
}

.brand-mark {
  display: block;
  color: var(--md-on-primary);
  text-decoration: none;
}

.brand-mark img {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  object-fit: contain;
  filter: none;
}

.brand-mark:hover {
  color: var(--md-on-primary);
}

.sidebar-nav {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
  margin-top: 12px;
  padding-right: 2px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.sidebar-nav::-webkit-scrollbar {
  width: 8px;
}

.sidebar-nav::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: rgba(95, 99, 104, 0.34);
}

.sidebar-group {
  padding: 4px 0 6px;
  border-bottom: 1px solid rgba(218, 220, 224, 0.72);
}

.sidebar-group:last-child {
  border-bottom: 0;
}

.sidebar-group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 36px;
  padding: 0 10px;
  color: var(--md-text);
  font-size: 0.98rem;
  font-weight: 700;
  text-align: left;
  border: 0;
  border-radius: var(--radius-control);
  background: transparent;
  cursor: pointer;
}

.sidebar-group-header:hover {
  background: rgba(60, 64, 67, 0.06);
}

.sidebar-group-chevron {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 2px;
  border-top: 2px solid currentColor;
  border-right: 2px solid currentColor;
  color: var(--md-text-secondary);
  transform: rotate(-45deg);
  transition: transform 140ms cubic-bezier(0.2, 0, 0, 1);
}

.sidebar-group-header[aria-expanded="false"] .sidebar-group-chevron {
  transform: rotate(135deg);
}

.sidebar-group-items {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.sidebar-group-items[hidden] {
  display: none;
}

.sidebar-link {
  position: relative;
  display: block;
  margin-bottom: 2px;
  padding: 8px 12px 8px 22px;
  border-radius: var(--radius-control);
  color: var(--md-text);
  text-decoration: none;
  transition: background-color 140ms cubic-bezier(0.2, 0, 0, 1), box-shadow 140ms cubic-bezier(0.2, 0, 0, 1);
}

.sidebar-link:hover {
  color: var(--md-text);
  background: var(--md-primary-surface);
}

.sidebar-link.active {
  color: var(--md-primary);
  background: var(--md-primary-container);
  font-weight: 700;
}

.sidebar-link.active::before {
  content: "";
  position: absolute;
  top: 7px;
  bottom: 7px;
  left: 0;
  width: 4px;
  border-radius: 0 4px 4px 0;
  background: var(--md-primary);
}

.sidebar-link-label {
  display: block;
  font-size: 0.94rem;
  font-weight: 600;
}

.sidebar-link-external {
  color: var(--md-text);
}

.sidebar-link-external:hover {
  color: var(--md-text);
}

.sidebar-link-external .sidebar-link-label::after {
  content: " ↗";
  font-size: 0.82rem;
  color: var(--md-text-secondary);
}

.app-main {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-width: 0;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 72px;
  padding: 12px 24px;
  background: var(--md-surface);
  border-bottom: 1px solid var(--md-outline);
  box-shadow: var(--elevation-1);
}

.app-header-leading {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.sidebar-toggle {
  display: none;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
}

.sidebar-toggle:hover {
  background: rgba(60, 64, 67, 0.08);
}

.sidebar-toggle span {
  display: block;
  width: 20px;
  height: 2px;
  margin: 4px auto;
  border-radius: 999px;
  background: var(--md-text);
}

.app-title-group {
  min-width: 0;
}

.app-eyebrow {
  color: var(--md-text-secondary);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.app-title {
  margin: 2px 0 0;
  color: var(--md-text);
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: 0;
}

.app-user {
  position: relative;
  flex: 0 0 auto;
}

.user-menu {
  position: relative;
}

.user-menu-trigger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  padding: 4px;
  border: 1px solid var(--md-outline);
  border-radius: 50%;
  background: var(--md-surface);
  box-shadow: var(--elevation-1);
}

.user-menu-trigger:hover {
  background: var(--md-surface-variant);
}

.user-menu-trigger:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--md-focus-ring);
}

.user-menu-panel {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  z-index: 80;
  min-width: 240px;
  padding: 14px;
  border: 1px solid var(--md-outline);
  border-radius: var(--radius-card);
  background: var(--md-surface);
  box-shadow: var(--elevation-3);
}

.user-avatar {
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 50%;
}

.user-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
  margin-bottom: 12px;
  color: var(--md-text);
  font-size: 0.875rem;
}

.user-meta strong {
  font-size: 0.9rem;
  font-weight: 600;
}

.user-meta span {
  white-space: nowrap;
}

.role-pill {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  margin-top: 1px;
  padding: 2px 8px;
  border-radius: 999px;
  color: var(--md-primary);
  font-size: 0.72rem;
  font-weight: 700;
  background: var(--md-primary-surface-strong);
}

.btn {
  position: relative;
  min-height: 40px;
  border-radius: var(--radius-control);
  font-weight: 600;
  transition: background-color 140ms cubic-bezier(0.2, 0, 0, 1), box-shadow 140ms cubic-bezier(0.2, 0, 0, 1), transform 140ms cubic-bezier(0.2, 0, 0, 1);
}

.btn:hover {
  transform: translateY(-1px);
}

.btn-brand,
.btn-primary {
  color: var(--md-on-primary);
  border: none;
  background: var(--md-primary);
  box-shadow: var(--elevation-1);
}

.btn-brand:hover,
.btn-primary:hover {
  color: var(--md-on-primary);
  background: var(--md-primary-hover);
  box-shadow: var(--elevation-2);
}

.btn-ghost,
.btn-outline-secondary,
.btn-secondary {
  color: var(--md-primary);
  border: 1px solid var(--md-outline);
  background: var(--md-surface);
}

.btn-ghost:hover,
.btn-outline-secondary:hover,
.btn-secondary:hover {
  color: var(--md-primary);
  background: var(--md-primary-surface);
}

.btn-danger {
  color: var(--md-on-primary);
  border-color: transparent;
  background: var(--md-error);
}

.btn-sm {
  min-height: 36px;
  padding-inline: 12px;
}

.app-content {
  padding: 24px;
}

.app-content-inner {
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-width: 1480px;
}

.panel {
  position: relative;
  padding: 20px;
  border: 1px solid var(--md-outline);
  border-radius: var(--radius-card);
  background: var(--md-surface);
  box-shadow: 0 1px 2px rgba(60, 64, 67, 0.18), 0 1px 3px rgba(60, 64, 67, 0.12);
}

.panel + .panel {
  margin-top: 0;
}

.panel-header {
  margin-bottom: 16px;
}

.panel-header h1,
.panel-header h2 {
  margin: 0;
  color: var(--md-text);
  letter-spacing: 0;
}

.panel-header h1 {
  font-size: 1.75rem;
  font-weight: 600;
}

.panel-header h2 {
  font-size: 1.1rem;
  font-weight: 600;
}

.panel-header p,
.muted {
  margin: 4px 0 0;
  color: var(--md-text-secondary);
  line-height: 1.55;
}

.inset-panel {
  height: 100%;
  padding: 16px;
  border-radius: var(--radius-card);
  background: var(--md-surface);
}

.stack-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form-grid {
  display: grid;
  gap: 14px;
}

.form-grid.two-columns {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.checkbox-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
}

.checkbox-card {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 12px;
  border: 1px solid var(--md-outline);
  border-radius: var(--radius-control);
  background: var(--md-surface);
  cursor: pointer;
}

.checkbox-card:hover {
  border-color: var(--md-primary);
  background: var(--md-primary-surface);
}

.checkbox-card input {
  width: auto;
  margin-top: 3px;
}

.checkbox-card span {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.checkbox-card small {
  color: var(--md-text-secondary);
  font-size: 0.8rem;
}

.form-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-row label {
  color: var(--md-text);
  font-size: 0.875rem;
  font-weight: 600;
}

.form-row input,
.form-row select,
.form-row textarea,
.inline-form select,
.inline-form input,
textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  color: var(--md-text);
  border: 1px solid var(--md-outline-strong);
  border-radius: var(--radius-control);
  background: var(--md-surface);
  transition: border-color 140ms cubic-bezier(0.2, 0, 0, 1), box-shadow 140ms cubic-bezier(0.2, 0, 0, 1);
}

.form-row textarea,
textarea {
  min-height: 160px;
  resize: vertical;
}

.form-row input:hover,
.form-row select:hover,
.form-row textarea:hover,
.inline-form select:hover,
textarea:hover {
  border-color: var(--md-primary);
}

.table-responsive {
  overflow-x: auto;
  border: 1px solid var(--md-outline);
  border-radius: var(--radius-card);
  background: var(--md-surface);
}

.table {
  margin-bottom: 0;
  vertical-align: middle;
}

.table > :not(caption) > * > * {
  padding: 12px 14px;
  border-color: var(--md-outline);
}

.table thead th {
  color: var(--md-text-secondary);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: var(--md-surface-variant);
}

.table tbody tr:hover {
  background: var(--md-primary-surface);
}

.table th:first-child,
.table td:first-child {
  width: 52px;
  white-space: nowrap;
}

.table th:last-child,
.table td:last-child {
  width: 76px;
  text-align: right;
  white-space: nowrap;
}

.table a {
  color: var(--md-primary);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

.admin-department-table th:last-child,
.admin-department-table td:last-child {
  width: auto;
  text-align: left;
}

.admin-department-table th:first-child,
.admin-department-table td:first-child {
  width: 180px;
}

.admin-users-grid {
  display: grid;
  grid-template-columns: minmax(320px, 430px) minmax(0, 1fr);
  gap: 16px;
}

.user-cell {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar.fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--md-on-primary);
  background: var(--md-primary);
  font-size: 1rem;
  font-weight: 700;
}

.user-name {
  color: var(--md-text);
  font-weight: 600;
}

.user-email {
  color: var(--md-text-secondary);
  font-size: 0.875rem;
}

.inline-form {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-chip {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 700;
}

.status-enabled {
  color: var(--md-success);
  background: rgba(19, 115, 51, 0.12);
}

.status-disabled {
  color: var(--md-error);
  background: rgba(179, 38, 30, 0.12);
}

.analytics-header,
.analytics-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.analytics-kpis {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) minmax(220px, 1fr) minmax(320px, 1fr);
  gap: 16px;
}

.kpi-card {
  padding: 16px;
  border: 1px solid var(--md-outline);
  border-radius: var(--radius-card);
  background: var(--md-surface);
  min-height: 196px;
  box-shadow: 0 1px 2px rgba(60, 64, 67, 0.18), 0 1px 3px rgba(60, 64, 67, 0.12);
  transition: box-shadow 140ms cubic-bezier(0.2, 0, 0, 1), transform 140ms cubic-bezier(0.2, 0, 0, 1);
}

.kpi-card:hover {
  transform: translateY(-1px);
  box-shadow: var(--elevation-2);
}

.kpi-label {
  margin-bottom: 8px;
  color: var(--md-text-secondary);
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.kpi-value {
  color: var(--md-text);
  font-size: 2.5rem;
  font-weight: 600;
  line-height: 1;
  letter-spacing: 0;
}

.kpi-subvalue {
  margin-top: 8px;
  color: var(--md-text-secondary);
  font-size: 0.875rem;
}

.chart-card {
  color: var(--md-primary);
}

.sparkline {
  width: 100%;
  height: 96px;
  margin-top: 10px;
  color: var(--md-primary);
  overflow: visible;
}

.analytics-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.analytics-grid-span-2 {
  grid-column: span 2;
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.image-preview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 12px;
}

.image-preview-card {
  padding: 8px;
  border: 1px solid var(--md-outline);
  border-radius: var(--radius-card);
  background: var(--md-surface);
  box-shadow: var(--elevation-1);
}

.image-preview-thumb {
  width: 100%;
  height: 148px;
  object-fit: cover;
  border-radius: var(--radius-control);
}

.image-preview-caption {
  margin-top: 8px;
  color: var(--md-text-secondary);
  font-size: 0.84rem;
  word-break: break-word;
}

.image-preview-remove {
  width: 100%;
  margin-top: 8px;
}

.social-image-result {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.chat-composer-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--md-outline);
  border-radius: var(--radius-card);
  background: var(--md-surface-subtle);
}

.chat-composer-panel.compact {
  padding: 0;
  border: 0;
  background: transparent;
}

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 240px;
  max-height: 420px;
  overflow-y: auto;
  padding: 12px;
  border: 1px solid var(--md-outline);
  border-radius: var(--radius-card);
  background: var(--md-surface);
}

.chat-message {
  max-width: min(760px, 86%);
  padding: 10px 12px;
  border-radius: 16px;
  line-height: 1.6;
  white-space: pre-wrap;
}

.chat-message.assistant {
  align-self: flex-start;
  color: var(--md-text-secondary);
  background: var(--md-surface-subtle);
}

.chat-message.user {
  align-self: flex-end;
  color: var(--md-on-primary);
  background: var(--md-primary);
}

.suggestion-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.suggestion-chip {
  border: 1px solid var(--md-outline);
  border-radius: 999px;
  padding: 7px 12px;
  color: var(--md-primary);
  font-weight: 700;
  background: var(--md-surface);
}

.suggestion-chip:hover,
.suggestion-chip:focus-visible {
  border-color: var(--md-primary);
  background: var(--md-primary-surface);
}

.chat-input-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
}

.social-image-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

.social-image-card {
  overflow: hidden;
  border: 1px solid var(--md-outline);
  border-radius: var(--radius-card);
  background: var(--md-surface);
  box-shadow: var(--elevation-1);
}

.social-image-card img {
  display: block;
  width: 100%;
  max-height: 680px;
  object-fit: contain;
  background: var(--md-surface-variant);
}

.social-image-card-meta {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
}

.social-image-card-meta span {
  color: var(--md-text-secondary);
  font-size: 0.86rem;
}

.generated-copy {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.generated-copy h3 {
  margin: 0;
  font-size: 1rem;
}

.hashtag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.alert {
  border: none;
  border-radius: var(--radius-card);
}

.alert-danger {
  color: var(--md-error);
  background: rgba(179, 38, 30, 0.1);
}

.alert-success {
  color: var(--md-success);
  background: rgba(19, 115, 51, 0.1);
}

.login-shell {
  display: grid;
  place-items: center;
  min-height: calc(100vh - 180px);
}

.login-panel {
  width: min(640px, 100%);
  padding: 24px;
}

.login-kicker {
  display: inline-flex;
  padding: 4px 10px;
  border-radius: 999px;
  color: var(--md-primary);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  background: var(--md-primary-surface-strong);
}

.page-lead {
  max-width: 720px;
}

@media (max-width: 1199.98px) {
  .analytics-kpis {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 991.98px) {
  .app-sidebar {
    position: fixed;
    left: 0;
    height: 100vh;
    max-height: 100vh;
    transform: translateX(-100%);
    transition: transform 180ms cubic-bezier(0.2, 0, 0, 1);
  }

  body.sidebar-open .app-sidebar {
    transform: translateX(0);
  }

  body.sidebar-open .app-sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar-toggle {
    display: inline-block;
  }

  .app-header {
    padding: 12px 16px;
  }

  .app-content {
    padding: 16px;
  }

  .user-meta {
    align-items: flex-start;
  }

  .admin-users-grid,
  .analytics-grid,
  .form-grid.two-columns {
    grid-template-columns: 1fr;
  }

  .analytics-grid-span-2 {
    grid-column: span 1;
  }

  .analytics-header,
  .analytics-meta,
  .toolbar-row,
  .inline-form,
  .chat-input-row {
    align-items: stretch;
    flex-direction: column;
  }

  .chat-input-row {
    display: flex;
  }
}

@media (max-width: 767.98px) {
  .app-header {
    flex-direction: column;
    align-items: stretch;
  }

  .app-header {
    flex-direction: row;
    align-items: center;
  }

  .user-menu-panel {
    right: 0;
    min-width: min(240px, calc(100vw - 32px));
  }

  .user-meta span,
  .user-meta strong {
    white-space: normal;
  }

  .panel {
    padding: 16px;
  }

  .login-panel {
    padding: 18px;
  }
}
