:root {
  color-scheme: light;
  --ink: #151817;
  --muted: #626a67;
  --subtle: #8a918e;
  --line: #dfe3e1;
  --line-strong: #c8cecb;
  --paper: #ffffff;
  --wash: #f5f7f6;
  --wash-strong: #ecefed;
  --charcoal: #202523;
  --green: #197149;
  --green-soft: #e8f4ed;
  --blue: #175bd7;
  --blue-dark: #1147a8;
  --blue-soft: #edf3ff;
  --amber: #946200;
  --amber-soft: #fff3d6;
  --red: #b6332c;
  --red-soft: #fff0ee;
  --focus: #0b62d8;
  --shadow: 0 18px 48px rgba(18, 27, 23, .18);
  font-family: Inter, "Noto Sans Thai", "Leelawadee UI", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  letter-spacing: 0;
}

* { box-sizing: border-box; }
html { min-width: 320px; scroll-behavior: smooth; }
body { min-height: 100vh; margin: 0; background: var(--wash); color: var(--ink); }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
a { color: inherit; }
[hidden] { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.eyebrow {
  margin: 0 0 7px;
  color: var(--blue);
  font-size: 11px;
  font-weight: 750;
  text-transform: uppercase;
}

.eyebrow.danger { color: var(--red); }

.brand-mark {
  display: grid;
  flex: 0 0 32px;
  width: 32px;
  height: 32px;
  place-items: center;
  border-radius: 6px;
  background: var(--charcoal);
  color: white;
  font-size: 11px;
  font-weight: 800;
}

.button {
  display: inline-flex;
  min-height: 40px;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 5px;
  padding: 8px 14px;
  background: white;
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.2;
  text-decoration: none;
  transition: background-color 140ms ease, border-color 140ms ease, color 140ms ease;
}

.button:hover { background: var(--wash-strong); }
.button:disabled { cursor: wait; opacity: .58; }
.button-primary { background: var(--blue); border-color: var(--blue); color: white; }
.button-primary:hover { background: var(--blue-dark); border-color: var(--blue-dark); }
.button-secondary { border-color: var(--line-strong); }
.button-quiet { min-height: 34px; padding-inline: 10px; border-color: var(--line); }
.button-danger { background: var(--red); border-color: var(--red); color: white; }
.button-danger:hover { background: #8f2722; border-color: #8f2722; }
.button-full { width: 100%; min-height: 46px; }

.button:focus-visible,
.icon-button:focus-visible,
.nav-item:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--focus) 24%, transparent);
  outline-offset: 2px;
}

.login-view {
  min-height: 100vh;
  display: grid;
  grid-template-columns: minmax(360px, 540px) minmax(380px, 1fr);
  background: var(--paper);
}

.login-panel {
  display: flex;
  min-height: 100vh;
  flex-direction: column;
  justify-content: center;
  border-right: 1px solid var(--line);
  padding: 56px clamp(36px, 7vw, 88px);
}

.login-brand {
  position: absolute;
  top: 32px;
  display: inline-flex;
  align-items: center;
  gap: 11px;
  font-size: 14px;
  font-weight: 750;
}

.login-copy { margin-bottom: 28px; }
.login-copy h1 { margin: 0 0 10px; font-size: 30px; line-height: 1.25; }
.login-copy p:last-child { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.7; }
.form-stack { display: grid; gap: 18px; }
.login-context {
  position: relative;
  display: flex;
  min-height: 100vh;
  align-items: flex-end;
  overflow: hidden;
  padding: 64px;
  background: var(--charcoal);
  color: white;
}

.context-grid {
  position: absolute;
  inset: 52px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 12px;
  opacity: .55;
}

.context-grid span { border: 1px solid #4d5753; background: #2a312e; }
.context-grid span:nth-child(2), .context-grid span:nth-child(6) { background: #1f5f43; }
.context-grid span:nth-child(4) { border-color: #5374b6; background: #26395d; }
.login-context > div:last-child { position: relative; z-index: 1; }
.context-label { margin: 0 0 7px; color: #aeb8b4; font-size: 11px; font-weight: 700; text-transform: uppercase; }
.context-title { margin: 0; font-size: 28px; font-weight: 700; }
.context-meta { margin: 8px 0 0; color: #bcc5c1; font-size: 13px; }

.app-shell { min-height: 100vh; }
.topbar {
  position: sticky;
  z-index: 20;
  top: 0;
  display: flex;
  height: 58px;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  padding: 0 20px;
  background: var(--paper);
}

.brand { display: inline-flex; align-items: center; gap: 10px; font-size: 14px; font-weight: 750; text-decoration: none; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.environment-badge { border-radius: 4px; padding: 4px 7px; background: var(--green-soft); color: var(--green); font-size: 10px; font-weight: 750; text-transform: uppercase; }
.user-block { display: inline-flex; align-items: center; gap: 8px; padding-left: 8px; border-left: 1px solid var(--line); }
.user-avatar { display: grid; width: 29px; height: 29px; place-items: center; border-radius: 50%; background: var(--wash-strong); font-size: 10px; font-weight: 800; }
.user-name { font-size: 12px; font-weight: 650; }

.workspace { display: grid; min-height: calc(100vh - 58px); grid-template-columns: 216px minmax(0, 1fr); }
.sidebar {
  position: sticky;
  top: 58px;
  display: flex;
  height: calc(100vh - 58px);
  flex-direction: column;
  justify-content: space-between;
  border-right: 1px solid var(--line);
  padding: 18px 12px;
  background: var(--paper);
}

.sidebar nav { display: grid; gap: 4px; }
.nav-item { display: flex; min-height: 40px; align-items: center; gap: 10px; border-radius: 5px; padding: 8px 11px; color: var(--muted); font-size: 13px; font-weight: 650; text-decoration: none; }
.nav-item:hover { background: var(--wash); color: var(--ink); }
.nav-item.active { background: var(--wash-strong); color: var(--ink); }
.nav-symbol { width: 20px; text-align: center; font-size: 16px; }
.sidebar-footer { display: grid; gap: 3px; padding: 10px 11px; color: var(--subtle); font-size: 10px; }

.main-content { width: min(1220px, 100%); padding: 34px 36px 72px; }
.page-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; margin-bottom: 24px; }
.page-heading h1 { margin: 0; font-size: 28px; line-height: 1.2; }
.page-subtitle { margin: 7px 0 0; color: var(--muted); font-size: 13px; }

.summary-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin-bottom: 30px; }
.summary-item { display: grid; min-width: 0; min-height: 104px; align-content: space-between; border: 1px solid var(--line); border-radius: 6px; padding: 17px 18px; background: var(--paper); }
.summary-label { display: flex; align-items: center; gap: 7px; color: var(--muted); font-size: 12px; }
.summary-value { font-size: 26px; line-height: 1; }
.status-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; }
.status-dot-green { background: #20a867; }
.status-dot-amber { background: #d69a25; }
.status-dot-gray { background: #8d9793; }

.stores-section { scroll-margin-top: 78px; }
.section-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin-bottom: 14px; }
.section-heading h2 { margin: 0; font-size: 18px; }
.section-heading p { margin: 5px 0 0; color: var(--muted); font-size: 12px; }
.filters { display: grid; grid-template-columns: minmax(220px, 1fr) 190px; gap: 10px; margin-bottom: 12px; }
.search-field input, .filter-field select { width: 100%; height: 40px; border: 1px solid var(--line-strong); border-radius: 5px; background: var(--paper); color: var(--ink); }
.search-field input { padding: 0 12px; }
.filter-field { position: relative; }
.filter-field label { position: absolute; z-index: 1; top: 6px; left: 10px; color: var(--subtle); font-size: 9px; font-weight: 700; text-transform: uppercase; pointer-events: none; }
.filter-field select { padding: 13px 32px 0 10px; font-size: 12px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 6px; background: var(--paper); }
.stores-table { width: 100%; min-width: 890px; border-collapse: collapse; table-layout: fixed; }
.stores-table th { height: 42px; padding: 0 13px; border-bottom: 1px solid var(--line); background: #f8f9f8; color: var(--muted); font-size: 10px; font-weight: 750; text-align: left; text-transform: uppercase; }
.stores-table th:nth-child(1) { width: 24%; }
.stores-table th:nth-child(2) { width: 11%; }
.stores-table th:nth-child(3) { width: 15%; }
.stores-table th:nth-child(4) { width: 21%; }
.stores-table th:nth-child(5) { width: 14%; }
.stores-table th:nth-child(6) { width: 15%; }
.stores-table td { height: 70px; padding: 10px 13px; border-bottom: 1px solid var(--line); vertical-align: middle; font-size: 12px; }
.stores-table tbody tr:last-child td { border-bottom: 0; }
.stores-table tbody tr:hover { background: #fbfcfb; }
.store-identity { display: grid; min-width: 0; grid-template-columns: 34px minmax(0, 1fr); gap: 10px; align-items: center; }
.store-monogram { display: grid; width: 34px; height: 34px; place-items: center; border-radius: 5px; background: var(--blue-soft); color: var(--blue); font-size: 11px; font-weight: 800; text-transform: uppercase; }
.store-name { display: block; overflow: hidden; font-size: 13px; font-weight: 720; text-overflow: ellipsis; white-space: nowrap; }
.store-domain { display: block; overflow: hidden; margin-top: 3px; color: var(--muted); font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.status-badge { display: inline-flex; width: fit-content; align-items: center; border-radius: 4px; padding: 5px 7px; font-size: 10px; font-weight: 750; white-space: nowrap; }
.status-active, .install-installed { background: var(--green-soft); color: var(--green); }
.status-draft { background: var(--blue-soft); color: var(--blue); }
.status-paused, .install-not_installed { background: var(--wash-strong); color: #59625e; }
.install-pending, .install-action_required { background: var(--amber-soft); color: var(--amber); }
.provider-list { display: flex; min-height: 24px; flex-wrap: wrap; gap: 4px; }
.provider-chip { border: 1px solid var(--line); border-radius: 4px; padding: 3px 6px; background: white; color: var(--muted); font-size: 9px; font-weight: 700; white-space: nowrap; }
.provider-empty { color: var(--subtle); font-size: 10px; }
.updated-at { color: var(--muted); white-space: nowrap; }
.row-actions { display: flex; justify-content: flex-end; gap: 5px; }
.row-action { min-height: 30px; border: 1px solid var(--line); border-radius: 4px; padding: 5px 8px; background: white; color: var(--ink); font-size: 10px; font-weight: 700; white-space: nowrap; }
.row-action:hover { background: var(--wash); }
.row-action.danger { color: var(--red); }

.loading-state, .empty-state { display: grid; min-height: 260px; place-items: center; align-content: center; border: 1px solid var(--line); border-radius: 6px; background: var(--paper); color: var(--muted); text-align: center; }
.loading-state { grid-template-columns: auto auto; gap: 10px; font-size: 13px; }
.spinner { width: 17px; height: 17px; border: 2px solid var(--line); border-top-color: var(--blue); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty-state h3 { margin: 12px 0 5px; color: var(--ink); font-size: 16px; }
.empty-state p { margin: 0 0 18px; font-size: 12px; }
.empty-mark { display: grid; width: 42px; height: 42px; place-items: center; border-radius: 6px; background: var(--wash-strong); color: var(--muted); font-size: 11px; font-weight: 800; }
.empty-state.compact { min-height: 190px; }
.empty-state.compact h3 { margin-top: 0; }

.dialog { width: min(720px, calc(100vw - 32px)); max-height: min(820px, calc(100vh - 32px)); overflow: hidden; border: 1px solid var(--line-strong); border-radius: 7px; padding: 0; background: var(--paper); color: var(--ink); box-shadow: var(--shadow); }
.dialog::backdrop { background: rgba(17, 22, 20, .54); }
.dialog form { display: grid; max-height: inherit; grid-template-rows: auto minmax(0, 1fr) auto; }
.dialog-header { display: flex; min-height: 70px; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); padding: 14px 18px; }
.dialog-header h2 { margin: 0; font-size: 18px; }
.icon-button { display: grid; width: 34px; height: 34px; place-items: center; border: 0; border-radius: 4px; background: transparent; color: var(--muted); font-size: 25px; line-height: 1; }
.icon-button:hover { background: var(--wash); color: var(--ink); }
.dialog-body { overflow-y: auto; padding: 0 18px; }
.form-section { padding: 20px 0; border-bottom: 1px solid var(--line); }
.form-section:last-child { border-bottom: 0; }
.form-section h3 { margin: 0 0 14px; font-size: 13px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { display: grid; min-width: 0; gap: 6px; }
.field-span-2 { grid-column: 1 / -1; }
.field label { color: #343937; font-size: 11px; font-weight: 700; }
.field input, .field select, .field textarea { width: 100%; border: 1px solid var(--line-strong); border-radius: 5px; background: white; color: var(--ink); }
.field input, .field select { height: 40px; padding: 0 10px; }
.field textarea { resize: vertical; padding: 10px; line-height: 1.5; }
.field input[aria-invalid="true"], .field select[aria-invalid="true"] { border-color: var(--red); }
.field-message { min-height: 14px; margin: 0; color: var(--red); font-size: 9px; }
.error-text { min-height: 0; font-size: 11px; }
.toggle-list { display: grid; border: 1px solid var(--line); border-radius: 5px; }
.toggle-row { position: relative; display: flex; min-height: 58px; align-items: center; justify-content: space-between; gap: 16px; padding: 10px 12px; cursor: pointer; }
.toggle-row + .toggle-row { border-top: 1px solid var(--line); }
.toggle-row span { display: grid; gap: 3px; }
.toggle-row strong { font-size: 11px; }
.toggle-row small { color: var(--muted); font-size: 9px; }
.toggle-row input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-row i { position: relative; display: block; flex: 0 0 34px; width: 34px; height: 19px; border-radius: 999px; background: #b9c0bd; transition: background-color 140ms ease; }
.toggle-row i::after { position: absolute; top: 3px; left: 3px; width: 13px; height: 13px; border-radius: 50%; background: white; content: ""; transition: transform 140ms ease; }
.toggle-row input:checked + i { background: var(--green); }
.toggle-row input:checked + i::after { transform: translateX(15px); }
.toggle-row input:focus-visible + i { outline: 3px solid color-mix(in srgb, var(--focus) 24%, transparent); outline-offset: 2px; }
.dialog-footer { display: flex; min-height: 66px; align-items: center; justify-content: space-between; gap: 14px; border-top: 1px solid var(--line); padding: 12px 18px; background: #fafbfa; }
.dialog-footer > div { display: flex; gap: 8px; }
.form-error { margin: 0; color: var(--red); font-size: 10px; }
.confirm-dialog { width: min(480px, calc(100vw - 32px)); }
.confirm-dialog .dialog-body { padding-block: 22px; color: var(--muted); font-size: 13px; line-height: 1.65; }
.confirm-dialog .dialog-body p { margin: 0; }

.toast { position: fixed; z-index: 100; right: 22px; bottom: 22px; max-width: min(360px, calc(100vw - 44px)); border: 1px solid #263a31; border-radius: 5px; padding: 12px 15px; background: #1f2d27; color: white; box-shadow: 0 12px 30px rgba(20, 28, 24, .22); font-size: 12px; }
.toast.error { border-color: #682b28; background: #542421; }

@media (max-width: 900px) {
  .workspace { grid-template-columns: 72px minmax(0, 1fr); }
  .sidebar { padding-inline: 8px; }
  .nav-item { justify-content: center; padding-inline: 8px; }
  .nav-item span:last-child, .sidebar-footer { display: none; }
  .main-content { padding-inline: 24px; }
  .summary-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 700px) {
  .login-view { grid-template-columns: 1fr; }
  .login-panel { min-height: 100vh; border-right: 0; padding: 84px 24px 48px; }
  .login-brand { top: 24px; }
  .login-context { display: none; }
  .topbar { padding-inline: 14px; }
  .brand-name, .environment-badge, .user-block { display: none; }
  .workspace { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 30; top: auto; right: 0; bottom: 0; left: 0; height: 58px; padding: 6px 12px; border-top: 1px solid var(--line); border-right: 0; }
  .sidebar nav { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .nav-item { min-height: 44px; gap: 6px; }
  .nav-item span:last-child { display: inline; }
  .main-content { padding: 24px 16px 88px; }
  .page-heading { align-items: flex-start; }
  .page-heading h1 { font-size: 24px; }
  .summary-grid { gap: 8px; }
  .summary-item { min-height: 92px; padding: 14px; }
  .summary-value { font-size: 23px; }
  .filters { grid-template-columns: 1fr; }
  .filter-field { width: 100%; }
  .table-wrap { overflow: visible; border: 0; background: transparent; }
  .stores-table, .stores-table tbody { display: block; min-width: 0; }
  .stores-table thead { display: none; }
  .stores-table tr { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 9px; border: 1px solid var(--line); border-radius: 6px; padding: 14px; background: var(--paper); }
  .stores-table td { display: block; height: auto; padding: 0; border: 0; }
  .stores-table td:first-child { grid-column: 1 / -1; }
  .stores-table td:nth-child(4), .stores-table td:nth-child(5) { grid-column: 1 / -1; }
  .stores-table td::before { display: block; margin-bottom: 6px; color: var(--subtle); font-size: 8px; font-weight: 750; text-transform: uppercase; }
  .stores-table td:nth-child(2)::before { content: "สถานะ"; }
  .stores-table td:nth-child(3)::before { content: "การติดตั้ง"; }
  .stores-table td:nth-child(4)::before { content: "วิธีเข้าสู่ระบบ"; }
  .stores-table td:nth-child(5)::before { content: "อัปเดตล่าสุด"; }
  .stores-table td:last-child { grid-column: 1 / -1; }
  .row-actions { justify-content: flex-start; padding-top: 4px; }
  .form-grid { grid-template-columns: 1fr; }
  .field-span-2 { grid-column: auto; }
  .dialog { width: calc(100vw - 20px); max-height: calc(100vh - 20px); }
  .dialog-footer { align-items: flex-start; flex-direction: column; }
  .dialog-footer > div { width: 100%; }
  .dialog-footer .button { flex: 1; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
