:root {
  --ink: #18212f;
  --muted: #627084;
  --line: #d8e0ea;
  --soft: #f4f7fa;
  --surface: #ffffff;
  --brand: #216e73;
  --brand-dark: #164d52;
  --accent: #b96f2d;
  --danger: #b42318;
  --ok: #15803d;
  --warn: #a15c07;
  --shadow: 0 12px 30px rgba(20, 32, 46, 0.08);
}

* {
  box-sizing: border-box;
}

body.portal-body {
  margin: 0;
  color: var(--ink);
  background: var(--soft);
  font-family: "Noto Sans JP", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.65;
}

a {
  color: var(--brand);
  overflow-wrap: anywhere;
}

.portal-shell {
  min-height: 100vh;
}

.portal-header {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
  backdrop-filter: blur(14px);
}

.portal-header__inner {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

.portal-brand {
  color: var(--ink);
  text-decoration: none;
  font-size: 18px;
  font-weight: 700;
}

.portal-nav {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  max-width: 100%;
}

.portal-nav a {
  color: var(--muted);
  text-decoration: none;
  font-size: 14px;
  border-radius: 999px;
  padding: 7px 10px;
  white-space: nowrap;
}

.portal-nav a:hover {
  background: #eef4f6;
  color: var(--brand-dark);
}

.portal-main {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 32px 0 56px;
}

.portal-auth {
  min-height: calc(100vh - 68px);
  display: grid;
  grid-template-columns: minmax(0, 1fr) 420px;
  gap: 36px;
  align-items: center;
}

.portal-auth__copy h1,
.page-head h1 {
  margin: 0 0 12px;
  font-size: clamp(30px, 5vw, 56px);
  line-height: 1.12;
  letter-spacing: 0;
}

.portal-auth__copy p,
.page-head p {
  margin: 0;
  color: var(--muted);
  font-size: 16px;
}

.panel,
.case-card,
.app-tile,
.message,
.next-action,
.table-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.panel {
  padding: 24px;
}

.panel h2,
.panel h3 {
  margin: 0 0 16px;
}

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

.form-row {
  display: grid;
  gap: 8px;
}

.form-row label {
  font-weight: 700;
  font-size: 14px;
}

input,
select,
textarea {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}

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

.button,
button {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 9px 14px;
  background: var(--brand);
  color: #fff;
  font: inherit;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
}

.button:hover,
button:hover {
  background: var(--brand-dark);
}

.button:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
a:focus-visible {
  outline: 3px solid rgba(33, 110, 115, 0.22);
  outline-offset: 2px;
}

.button.secondary,
button.secondary {
  background: #fff;
  color: var(--ink);
  border-color: var(--line);
}

.button.secondary:hover,
button.secondary:hover {
  background: var(--soft);
}

.button.warning,
button.warning {
  background: var(--accent);
}

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

.button.compact,
button.compact {
  min-height: 34px;
  padding: 6px 10px;
  font-size: 13px;
}

.notice,
.error {
  border-radius: 6px;
  padding: 12px 14px;
  margin: 0 0 18px;
  border: 1px solid;
}

.notice {
  color: #0d4b2b;
  background: #eaf7ef;
  border-color: #b7e2c4;
}

.error {
  color: #7b1e16;
  background: #fff0ee;
  border-color: #f1b8b1;
}

.page-head {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: flex-end;
  margin: 0 0 24px;
}

.grid-2 {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 24px;
  align-items: start;
}

.password-setup-panel {
  width: min(640px, 100%);
  margin: 64px auto;
}

.password-setup-panel h1 {
  margin: 0 0 12px;
  font-size: 32px;
  line-height: 1.25;
}

.case-list,
.app-grid,
.stats-grid {
  display: grid;
  gap: 14px;
}

.case-list {
  grid-template-columns: 1fr;
}

.app-grid,
.stats-grid {
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
}

.case-card,
.app-tile {
  padding: 18px;
}

.app-tile {
  display: grid;
  gap: 12px;
  align-content: start;
}

.case-card__top {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
}

.case-card h3,
.app-tile h3 {
  margin: 0 0 8px;
  font-size: 18px;
}

.case-card-actions {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.case-meta,
.muted {
  color: var(--muted);
  font-size: 13px;
}

.badge-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  border-radius: 999px;
  padding: 3px 10px;
  background: #eef4f6;
  color: var(--brand-dark);
  font-size: 12px;
  font-weight: 700;
}

.badge.ok {
  background: #e7f5ec;
  color: var(--ok);
}

.badge.warn {
  background: #fff4df;
  color: var(--warn);
}

.badge.danger {
  background: #fff0ee;
  color: var(--danger);
}

.badge.muted {
  background: #eef1f5;
  color: var(--muted);
}

.section-kicker {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  margin-bottom: 4px;
}

.next-action {
  border-left: 4px solid var(--brand);
  box-shadow: var(--shadow);
  margin: 0 0 18px;
  padding: 18px 20px;
}

.next-action h2 {
  margin: 0 0 6px;
  font-size: 20px;
  line-height: 1.35;
}

.next-action p {
  margin: 0;
  color: var(--muted);
}

.next-action.ok {
  border-left-color: var(--ok);
  background: #f6fbf8;
}

.next-action.warn {
  border-left-color: var(--warn);
  background: #fffaf1;
}

.next-action.danger {
  border-left-color: var(--danger);
  background: #fff7f6;
}

.next-action.muted {
  border-left-color: #98a2b3;
  background: #f8fafc;
}

.admin-next-action {
  margin-bottom: 18px;
}

.case-next-inline {
  display: grid;
  gap: 8px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.case-next-inline p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.template-block {
  display: grid;
  gap: 12px;
  margin: 0 0 18px;
}

.template-block p {
  margin: 0;
}

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

button.template-chip {
  min-height: 38px;
  justify-content: center;
  border-color: var(--line);
  background: #fff;
  color: var(--ink);
  padding: 7px 10px;
  font-size: 13px;
}

button.template-chip:hover {
  border-color: var(--brand);
  background: #edf7f6;
}

.archive-panel {
  margin-top: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.archive-panel summary {
  cursor: pointer;
  padding: 14px 16px;
  color: var(--muted);
  font-weight: 700;
}

.archive-list {
  padding: 0 16px 16px;
}

.row-next {
  margin-top: 8px;
}

.chat-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 24px;
  align-items: start;
}

.message-list {
  display: grid;
  gap: 12px;
  margin-bottom: 20px;
}

.message {
  padding: 14px 16px;
  box-shadow: none;
}

.message.customer {
  border-left: 4px solid var(--brand);
}

.message.staff {
  border-left: 4px solid var(--accent);
}

.message.system {
  border-left: 4px solid #667085;
  background: #fbfcfd;
}

.message.internal {
  border-style: dashed;
  background: #fffaf2;
}

.message__head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 6px;
}

.message__body {
  white-space: pre-wrap;
}

.message__body a {
  color: var(--brand);
  font-weight: 700;
  overflow-wrap: anywhere;
}

.message-attachments {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 220px));
  gap: 10px;
  margin-top: 12px;
}

.message-attachment {
  display: grid;
  gap: 8px;
  align-content: start;
  min-width: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  text-decoration: none;
}

.message-attachment:hover {
  border-color: rgba(31, 94, 255, 0.35);
}

.message-attachment img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border-radius: 6px;
  background: #f3f5f8;
}

.message-attachment__meta {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.message-attachment__meta strong,
.message-attachment__meta small {
  overflow-wrap: anywhere;
}

.message-attachment__meta small {
  color: var(--muted);
  font-size: 12px;
}

.message-delete-form {
  margin-top: 8px;
}

.link-button {
  border: 0;
  background: transparent;
  padding: 0;
  min-height: 0;
  color: var(--muted);
  font: inherit;
  text-decoration: underline;
  cursor: pointer;
}

.danger-link {
  color: #b42318;
}

.estimate-choice-panel,
.payment-action-panel {
  margin-bottom: 18px;
}

.thread-actions {
  display: grid;
  gap: 10px;
  margin: 12px 0;
}

.admin-thread-actions {
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  align-items: end;
}

.thread-actions form {
  margin: 0;
}

.file-list {
  display: grid;
  gap: 10px;
  padding: 0;
  margin: 0;
  list-style: none;
}

.file-list li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 10px;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
}

.admin-table th,
.admin-table td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  font-size: 14px;
}

.admin-table th {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0;
  background: #f8fafc;
}

.table-panel {
  overflow-x: auto;
  box-shadow: none;
}

.admin-table a {
  font-weight: 700;
}

.stat-card {
  display: grid;
  gap: 6px;
}

.stat-value {
  display: block;
  color: var(--ink);
  font-size: 26px;
  line-height: 1.15;
}

.estimate-workbench {
  padding: 0;
}

.empty-state {
  padding: 28px;
}

.empty-state h2 {
  margin: 0 0 8px;
  font-size: 22px;
}

.estimate-row-title {
  display: grid;
  gap: 8px;
}

.money-strong {
  display: inline-block;
  color: var(--ink);
  font-size: 18px;
  white-space: nowrap;
}

.action-stack {
  display: grid;
  gap: 10px;
  align-items: start;
  justify-items: start;
}

.quick-pay-form {
  display: grid;
  gap: 8px;
  min-width: 220px;
}

.quick-pay-form input {
  min-height: 36px;
  padding: 7px 10px;
  font-size: 13px;
}

.panel-heading {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: center;
  padding: 18px;
  border-bottom: 1px solid var(--line);
}

.panel-heading h2 {
  margin: 0;
  font-size: 20px;
}

.agent-workbench {
  padding: 0;
}

.agent-log-text {
  max-width: 520px;
  max-height: 160px;
  overflow: auto;
  white-space: pre-wrap;
  color: var(--ink);
}

.agent-rule-list {
  display: grid;
  gap: 12px;
}

.agent-rule-list p {
  margin: 0;
}

.agent-rule-editor {
  display: grid;
  gap: 12px;
  margin-top: 18px;
}

.agent-rule-card {
  display: grid;
  grid-template-columns: 160px minmax(240px, 1fr) 140px 160px minmax(180px, auto) auto;
  gap: 12px;
  align-items: end;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.agent-rule-card.new-rule {
  background: #f8fafc;
}

.agent-rule-card__main {
  min-width: 0;
}

.agent-rule-toggles {
  display: grid;
  gap: 8px;
  align-content: end;
}

.admin-columns {
  display: grid;
  grid-template-columns: 430px minmax(0, 1fr);
  gap: 22px;
  align-items: start;
}

.tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 0 0 18px;
}

.tabs a {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 12px;
  color: var(--muted);
  text-decoration: none;
  background: #fff;
  font-size: 13px;
}

.tabs a.active {
  color: #fff;
  background: var(--brand);
  border-color: var(--brand);
}

.split-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}

details.admin-login {
  margin-top: 20px;
  border-top: 1px solid var(--line);
  padding-top: 18px;
}

details.admin-login summary {
  cursor: pointer;
  color: var(--muted);
  font-weight: 700;
}

@media (max-width: 900px) {
  .portal-auth,
  .grid-2,
  .chat-layout,
  .agent-rule-card,
  .admin-columns {
    grid-template-columns: 1fr;
  }

  .page-head {
    align-items: flex-start;
    flex-direction: column;
  }
}

@media (max-width: 760px) {
  .table-panel {
    overflow: visible;
    border: 0;
    background: transparent;
  }

  .admin-table,
  .admin-table thead,
  .admin-table tbody,
  .admin-table tr,
  .admin-table td {
    display: block;
    width: 100%;
  }

  .admin-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
  }

  .admin-table {
    background: transparent;
    border-collapse: separate;
    border-spacing: 0 12px;
  }

  .admin-table tr {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
    padding: 12px;
  }

  .admin-table td {
    border: 0;
    padding: 7px 0;
  }

  .admin-table td::before {
    content: attr(data-label);
    display: block;
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    margin-bottom: 2px;
  }

  .admin-table td:empty {
    display: none;
  }
}

@media (max-width: 640px) {
  .portal-header__inner,
  .portal-main {
    width: min(100% - 24px, 1180px);
  }

  .portal-header__inner {
    align-items: flex-start;
    padding: 14px 0;
    flex-direction: column;
    gap: 12px;
  }

  .portal-brand {
    font-size: 16px;
  }

  .portal-nav {
    width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 2px;
  }

  .portal-nav a {
    flex: 0 0 auto;
    background: #f8fafc;
  }

  .portal-auth__copy h1,
  .page-head h1 {
    font-size: 30px;
    overflow-wrap: anywhere;
  }

  .panel {
    padding: 18px;
  }

  .page-head {
    margin-bottom: 18px;
  }

  .button,
  button {
    width: 100%;
  }

  .split-actions,
  .action-stack,
  .quick-pay-form,
  .panel-heading {
    width: 100%;
  }

  .panel-heading {
    align-items: stretch;
    flex-direction: column;
  }

  .message__head {
    display: grid;
    gap: 2px;
  }

  .file-list li {
    align-items: flex-start;
    flex-direction: column;
  }

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

  .next-action {
    padding: 16px;
  }

  .case-card__top {
    flex-direction: column;
  }
}
