* {
  box-sizing: border-box;
}

:root {
  --ink: #153638;
  --muted: #627476;
  --line: #d6e1df;
  --surface: #ffffff;
  --canvas: #f3f7f6;
  --teal: #085d62;
  --teal-dark: #064b4f;
  --teal-soft: #e9f5f4;
  --orange: #fb9d23;
  --orange-dark: #a84f00;
  --orange-soft: #fff2df;
  --blue-soft: #eaf2ff;
  --danger: #b42318;
  --shadow: 0 10px 30px rgba(8, 93, 98, 0.08);
}

html,
body {
  width: 100%;
  height: 100%;
}

body {
  margin: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
  background: var(--canvas);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app-header {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 11px 18px;
  background: var(--surface);
  border-bottom: 3px solid var(--orange);
  box-shadow: 0 1px 0 rgba(16, 24, 40, 0.02);
  position: relative;
  z-index: 10;
}

.brand {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 11px;
}

.brand-logo {
  width: 154px;
  height: 44px;
  flex: 0 0 154px;
  display: flex;
  align-items: center;
  border-radius: 8px;
  outline: none;
}

.brand-logo img {
  display: block;
  width: 100%;
  height: auto;
}

.brand-logo:focus-visible {
  box-shadow: 0 0 0 3px rgba(251, 157, 35, 0.35);
}

.board-badge {
  padding: 5px 9px;
  border-radius: 999px;
  background: var(--orange-soft);
  color: var(--orange-dark);
  font-size: 11px;
  font-weight: 750;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.actions,
.code-actions {
  display: flex;
  align-items: center;
  gap: 7px;
}

.button {
  min-height: 38px;
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 680;
  transition: transform 130ms ease, background 130ms ease, border-color 130ms ease;
  white-space: nowrap;
}

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

.button:active {
  transform: translateY(0);
}

.button.ghost {
  background: white;
  border-color: var(--line);
  color: #315052;
}

.button.ghost:hover {
  background: var(--teal-soft);
  border-color: #b5d6d3;
}

.button.secondary {
  color: var(--teal-dark);
  background: var(--teal-soft);
  border-color: #b7dcd8;
}

.button.primary {
  color: white;
  background: var(--teal);
  border-color: var(--teal);
  box-shadow: 0 5px 12px rgba(8, 93, 98, 0.2);
}

.button.primary:hover {
  background: var(--teal-dark);
}

.workspace-layout {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(520px, 1fr) minmax(380px, 0.42fr);
  gap: 10px;
  padding: 10px;
}

.workspace-panel,
.code-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 13px;
  box-shadow: var(--shadow);
}

.workspace-panel {
  grid-template-rows: 46px minmax(0, 1fr);
}

.code-panel {
  grid-template-rows: 46px minmax(0, 1fr) 38px;
}

.panel-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 13px 0 15px;
  border-bottom: 1px solid var(--line);
  background: #f8fbfa;
}

.panel-bar > div:first-child {
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.panel-bar strong {
  font-size: 13px;
  color: var(--ink);
}

.panel-bar span {
  color: var(--muted);
  font-size: 11px;
}

.text-button,
.icon-button {
  border: 0;
  cursor: pointer;
  color: var(--teal-dark);
  background: transparent;
  font-size: 12px;
  font-weight: 700;
  border-radius: 7px;
}

.text-button {
  padding: 6px 8px;
}

.text-button:hover,
.icon-button:hover {
  background: var(--teal-soft);
}

.text-button.danger {
  color: var(--danger);
}

.text-button.danger:hover {
  background: #fef3f2;
}

#blocklyDiv {
  min-width: 0;
  min-height: 0;
  width: 100%;
  height: 100%;
}

#codeArea {
  min-width: 0;
  min-height: 0;
  margin: 0;
  overflow: auto;
  padding: 18px;
  border: 0;
  outline: 0;
  background: #111827;
  color: #d8e7f5;
  tab-size: 2;
  white-space: pre;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 12.5px;
  line-height: 1.62;
}

#codeArea:focus-visible {
  box-shadow: inset 0 0 0 2px var(--orange);
}

.code-footer {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
  padding: 0 12px;
  background: #0b1220;
  color: #95a4b8;
  font-size: 11px;
  border-top: 1px solid #263244;
}

.code-footer span {
  display: flex;
  align-items: center;
  gap: 6px;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #36b37e;
  box-shadow: 0 0 0 3px rgba(54, 179, 126, 0.14);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 1000;
  max-width: calc(100vw - 32px);
  padding: 10px 15px;
  border-radius: 10px;
  background: #17202a;
  color: white;
  box-shadow: 0 12px 32px rgba(16, 24, 40, 0.25);
  font-size: 13px;
  font-weight: 650;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 12px);
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.dialog {
  width: min(880px, calc(100vw - 32px));
  max-height: min(760px, calc(100vh - 32px));
  margin: auto;
  padding: 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: white;
  color: var(--ink);
  box-shadow: 0 24px 80px rgba(16, 24, 40, 0.28);
}

.dialog::backdrop {
  background: rgba(15, 23, 42, 0.56);
  backdrop-filter: blur(3px);
}

.dialog-heading {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px 15px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(8px);
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--orange-dark);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.dialog h2,
.dialog h3 {
  margin: 0;
  letter-spacing: -0.01em;
}

.dialog h2 {
  font-size: 21px;
}

.dialog h3 {
  margin-bottom: 12px;
  font-size: 14px;
}

.icon-button {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  color: #475467;
  font-size: 23px;
}

.guide-grid {
  display: grid;
  grid-template-columns: 0.88fr 1.12fr;
  gap: 24px;
  padding: 22px 20px 16px;
}

.guide-grid ol {
  margin: 0;
  padding-left: 20px;
  color: #475467;
  font-size: 13px;
  line-height: 1.65;
}

.guide-grid li + li {
  margin-top: 5px;
}

.copy-field {
  width: 100%;
  margin-top: 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px;
  border: 1px solid #b7dcd8;
  border-radius: 9px;
  background: var(--teal-soft);
  color: var(--teal-dark);
  cursor: pointer;
}

.copy-field code {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 10px;
  white-space: nowrap;
}

.copy-field span {
  font-size: 11px;
  font-weight: 800;
}

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

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}

th,
td {
  padding: 7px 9px;
  text-align: left;
  border-bottom: 1px solid #e8edf2;
}

th {
  background: #f8fafc;
  color: #475467;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

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

td:first-child {
  color: var(--teal-dark);
  font-weight: 800;
}

.notice {
  margin: 0 20px 20px;
  padding: 12px 14px;
  border: 1px solid #fedf89;
  border-radius: 10px;
  background: #fffaeb;
  color: #7a2e0e;
  font-size: 12px;
  line-height: 1.55;
}

.example-dialog {
  width: min(720px, calc(100vw - 32px));
}

.example-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  padding: 20px;
}

.example-card {
  min-height: 150px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 7px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: white;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  transition: transform 140ms ease, border-color 140ms ease, box-shadow 140ms ease;
}

.example-card:hover {
  transform: translateY(-2px);
  border-color: var(--orange);
  box-shadow: 0 9px 22px rgba(16, 24, 40, 0.08);
}

.example-card strong {
  margin-top: auto;
  font-size: 14px;
}

.example-card small {
  color: var(--muted);
  line-height: 1.4;
}

.example-icon {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 850;
}

.example-icon.teal {
  color: var(--teal-dark);
  background: var(--teal-soft);
}

.example-icon.orange {
  color: var(--orange-dark);
  background: var(--orange-soft);
}

.example-icon.blue {
  color: #285aa5;
  background: var(--blue-soft);
}

/* Blockly surface refinements */
.blocklyToolboxDiv {
  border-right: 1px solid var(--line) !important;
  box-shadow: none !important;
}

.blocklyTreeRow {
  height: 36px !important;
  margin: 3px 7px !important;
  border-radius: 7px !important;
  padding: 0 8px !important;
}

.blocklyTreeSelected {
  background-color: var(--teal-soft) !important;
}

.blocklyTreeSelected .blocklyTreeLabel {
  color: var(--teal-dark) !important;
  font-weight: 750 !important;
}

.blocklyTreeLabel {
  font-size: 12px !important;
}

.blocklyFlyout {
  filter: drop-shadow(4px 0 10px rgba(16, 24, 40, 0.06));
}

.blocklyScrollbarHandle {
  rx: 5;
}

.blocklyTreeLabel.sensor-label {
  display: inline-block;
  margin: 2px 0 4px;
  padding: 3px 5px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 4px;
  font-weight: 750;
  text-decoration: underline;
}

.blocklyTreeLabel.basic {
  background-color: #e0f7fa;
}

.blocklyTreeLabel.environmental {
  background-color: #e8f5e9;
}

.blocklyTreeLabel.motion {
  background-color: #fff3e0;
}

.blocklyTreeLabel.security {
  background-color: #fce4ec;
}

.blocklyTreeLabel.positioning {
  background-color: #ede7f6;
}

@media (max-width: 1120px) {
  .board-badge {
    display: none;
  }

  .button {
    padding-inline: 9px;
  }

  .workspace-layout {
    grid-template-columns: minmax(470px, 1fr) minmax(340px, 0.48fr);
  }
}

@media (max-width: 860px) {
  body {
    overflow: auto;
  }

  .app-header {
    align-items: flex-start;
    padding: 10px 12px;
  }

  .actions {
    max-width: 58vw;
    overflow-x: auto;
    padding-bottom: 3px;
  }

  .workspace-layout {
    height: max(980px, calc(100vh - 67px));
    grid-template-columns: 1fr;
    grid-template-rows: minmax(560px, 1fr) 410px;
  }

  .workspace-panel,
  .code-panel {
    box-shadow: none;
  }

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

@media (max-width: 600px) {
  .brand-logo {
    width: 132px;
    height: 38px;
    flex-basis: 132px;
  }

  .actions {
    max-width: calc(100vw - 162px);
  }

  .button.secondary {
    display: none;
  }

  .workspace-layout {
    padding: 6px;
    gap: 6px;
  }

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

  .example-card {
    min-height: 105px;
  }

  .dialog {
    width: calc(100vw - 16px);
    max-height: calc(100vh - 16px);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}
