:root{
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #e6e8ee;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #2563eb;
  --kbd: #f3f4f6;
  --kbd2:#e5e7eb;
  --danger:#ef4444;
  --ok:#10b981;
  --shadow: 0 8px 20px rgba(0,0,0,.06);
  --radius: 14px;
}

*{ box-sizing:border-box; }
html,body{ height:100%; }
body{
  margin:0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji","Segoe UI Emoji";
  background: var(--bg);
  color: var(--text);
  overflow:hidden;
}

.app{
  width:100%;
  height:100%;
  position: relative;
  min-width: 0;
}

.main{
  width:100%;
  height:100%;
  display: flex;
  align-items: stretch;
  padding: 12px;
  min-width: 0;
  min-height: 0;
  gap: 0;
  position: relative;
}

.left{
  width: var(--left-width, 360px);
  min-width: 200px;
  max-width: 70%;
  flex-shrink: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
  overflow: hidden;
  transition: width .22s ease, min-width .22s ease, opacity .18s ease, transform .22s ease, margin-right .22s ease;
}

.main.left-collapsed .left{
  width: 0;
  min-width: 0;
  border-width: 0;
  box-shadow: none;
  opacity: 0;
  transform: translateX(calc(-100% - 18px));
  margin-right: 0;
  pointer-events: none;
}

.left-header{
  display:flex;
  align-items:center;
  justify-content:space-between;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}

.title{
  font-weight: 700;
  font-size: 14px;
}

.left-actions{
  display:flex;
  gap:8px;
  align-items:center;
}

.btn{
  border:1px solid var(--line);
  background:#fff;
  padding: 6px 10px;
  border-radius: 10px;
  cursor:pointer;
  font-size:12px;
  color: var(--text);
}
.btn:hover{ border-color:#cfd4df; }
.btn.primary{
  background: var(--accent);
  border-color: var(--accent);
  color:#fff;
}
.btn.danger{
  background:#fff;
  border-color:#fecaca;
  color: var(--danger);
}

#btnAdd{
  width: 56px;
  min-width: 56px;
  height: 28px;
  padding: 0;
  border: 1px solid #2f6fdc;
  border-radius: 6px;
  background: #2f6fdc;
  color: transparent;
  font-size: 0;
  font-weight: 700;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.2), 0 1px 1px rgba(15, 23, 42, .15);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#btnAdd::before{
  content: "↩";
  color: #ffffff;
  font-size: 20px;
  line-height: 1;
}

#btnAdd:hover{
  background: #2a65ca;
  border-color: #2a65ca;
}

#btnAdd:active{
  transform: translateY(1px);
  background: #245ab4;
  border-color: #245ab4;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.16), 0 0 0 rgba(0,0,0,0);
}

.expr-list{
  padding: 6px;
  overflow:auto;
  min-height:0;
}

.expr-item{
  display:grid;
  grid-template-columns: auto 1fr 34px;
  align-items:center;
  gap: 6px;
  padding: 6px;
  border-radius: 12px;
  cursor:pointer;
  border: 1px solid transparent;
}
.expr-item:hover{
  background:#fafafa;
  border-color: var(--line);
}
.expr-item.active{
  background: rgba(37,99,235,.08);
  border-color: rgba(37,99,235,.25);
}

.expr-item--duplicate{
  border-color: rgba(239,68,68,.38);
  background: rgba(254,226,226,.42);
}

.expr-index{
  min-width: 28px;
  height: 28px;
  padding: 0 8px;
  border-radius: 10px;
  display:flex;
  align-items:center;
  justify-content:center;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  border:1px solid var(--line);
  background:#fff;
  white-space: nowrap;
  cursor: grab;
  user-select: none;
}

.expr-index:active{
  cursor: grabbing;
}

.expr-item--dragging{
  opacity: .65;
}

.expr-item--drop-target{
  outline: 2px dashed rgba(37,99,235,.45);
  outline-offset: 2px;
}

.expr-input{
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 13px;
  line-height: 1.2;
  padding: 10px 12px;
  border-radius: 10px;
  border:1px solid var(--line);
  background:#fff;
  outline:none;
  min-height: 32px;
}

.expr-input--code{
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 12.5px;
  background: #f7f8fb;
  border-color: #d9deea;
}

.expr-input:focus{
  border-color: rgba(37,99,235,.6);
  box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}

.expr-input--custom-kbd{
  caret-color: var(--accent);
}

.expr-input--invalid{
  border-color: rgba(239,68,68,.7);
  box-shadow: 0 0 0 2px rgba(239,68,68,.12);
}

.expr-input--invalid:focus{
  border-color: rgba(239,68,68,.9);
  box-shadow: 0 0 0 3px rgba(239,68,68,.18);
}

.expr-del{
  width: 28px;
  height: 28px;
  border-radius: 10px;
  border:1px solid var(--line);
  background:#fff;
  cursor:pointer;
  color: var(--muted);
}
.expr-del:hover{ color: var(--danger); border-color:#fecaca; }

/* Right graph area */
.right{
  flex: 1;
  width: 100%;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.pill{
  padding: 4px 8px;
  border-radius: 999px;
  border:1px solid var(--line);
  background:#fff;
  color: var(--muted);
}

.kbd-header{
  display: flex;
  align-items: center;
  padding: 0;
  border: none;
  background: transparent;
  width: fit-content;
}

.kbd-toggle{
  width: 38px;
  height: 28px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}

.kbd-body{
  max-height: 240px;
  overflow: auto;
}

.kbd-area{
  background: #eef1f6;
  border: 1px solid #d9dee8;
  border-radius: 12px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height:0;
}

.kbd-section{
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.kbd-section-title{
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: #64748b;
  padding-left: 2px;
}

.kbd-row{
  display:grid;
  gap: 6px;
  grid-template-columns: repeat(auto-fit, minmax(42px, 1fr));
}

.key{
  min-height: 36px;
  background: linear-gradient(180deg, #fdfdfe 0%, #eceff5 100%);
  border:1px solid #d6dce7;
  border-radius: 10px;
  padding: 8px 8px;
  text-align:center;
  font-size: 18px;
  color: #2f3645;
  cursor:pointer;
  user-select:none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.9), 0 1px 1px rgba(30, 41, 59, .08);
  transition: border-color .15s ease, background .15s ease, transform .06s ease;
}
.key:hover{
  border-color:#c4ccda;
  background: linear-gradient(180deg, #ffffff 0%, #e8ecf4 100%);
}
.key:active{
  transform: translateY(1px);
}
.key.op{
  font-weight: 600;
  background: linear-gradient(180deg, #eef1f6 0%, #dce2eb 100%);
  border-color: #c7d0dd;
  color: #263142;
}
.key.op:hover{
  background: linear-gradient(180deg, #f2f5fa 0%, #d5dde9 100%);
  border-color: #b6c1d0;
}

.key.var{
  background: linear-gradient(180deg, #ffffff 0%, #f4f6fb 100%);
  border-color: #d8deea;
}

.key.ref{
  background: linear-gradient(180deg, #ffffff 0%, #f4f6fb 100%);
  border-color: #d8deea;
  color: #2f3645;
  font-weight: 700;
}
.key.ref:hover{
  background: linear-gradient(180deg, #ffffff 0%, #edf2fa 100%);
  border-color: #c7d0df;
}

.key.fn{
  background: linear-gradient(180deg, #edf1f8 0%, #dfe5f0 100%);
  border-color: #cfd6e4;
  font-weight: 700;
}

.key-fn-template{
  justify-content: flex-start;
  text-align: left;
  font-size: 12px;
  line-height: 1.25;
  padding: 10px 12px;
  min-height: 42px;
  white-space: normal;
  word-break: break-word;
}

@media (max-width: 980px){
  .main{ flex-direction: column; }
  .left{ max-width: none; width: 100% !important; }
  .main.left-collapsed .left{
    width: 0 !important;
    height: 0;
    min-width: 0;
    min-height: 0;
    flex-basis: 0;
    margin: 0;
    overflow: hidden;
  }
  .main.left-collapsed .right{
    flex: 1 1 auto;
    width: 100%;
    min-width: 0;
    min-height: 0;
  }
  .resizer{ display: none; }
  .keyboard{ grid-template-columns: 1fr; }
}

.chip{
  display:flex;
  align-items:center;
  gap:6px;
  padding: 4px 8px;
  border:1px solid var(--line);
  border-radius: 999px;
  background:#fff;
  font-size:12px;
  color: var(--muted);
  cursor: grab;
}

.chip:active{
  cursor: grabbing;
}

.chip.dragging{
  opacity: 0.55;
}

.chip.drop-target{
  outline: 2px dashed rgba(37,99,235,.45);
  outline-offset: 2px;
}

.chip-btn{
  border:1px solid var(--line);
  background:#fff;
  border-radius: 8px;
  font-size:11px;
  cursor:pointer;
  padding: 0 6px;
}
.chip-btn:hover{
  border-color:#cfd4df;
}

.expr-item{
  grid-template-columns: auto 1fr 34px;
}

.expr-mid{
  display:flex;
  flex-direction:column;
  gap: 6px;
  min-width:0;
}

.order-bar{
  display:none;
  flex-wrap:wrap;
  gap:6px;
  padding-left: 2px;
}

.expr-reduce-btn{
  min-width: 28px;
  height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.expr-reduce-btn:hover{
  border-color:#cfd4df;
  background:#f8fafc;
}

.expr-reduce-btn:disabled{
  opacity: .45;
  cursor: not-allowed;
  background: #f3f4f6;
}

.expr-reduce-btn.reduce-available,
.expr-reduce-btn.reduce-available:disabled{
  border-color: #15803d;
  background: #22c55e;
  color: #ffffff;
  opacity: 1;
}

.expr-reduce-btn.reduce-unavailable,
.expr-reduce-btn.reduce-unavailable:disabled{
  border-color: #b91c1c;
  background: #ef4444;
  color: #ffffff;
  opacity: 1;
}

.expr-apply-ui{
  display:none;
  flex-direction:column;
  gap:8px;
  padding-left: 2px;
}

.expr-apply-hint{
  font-size:12px;
  color: var(--muted);
}

.expr-apply-actions{
  display:flex;
  gap:8px;
  flex-wrap:wrap;
}

.expr-invalid-hint{
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid #fecaca;
  background: #fff1f2;
  color: #b91c1c;
  font-size: 12px;
  font-weight: 600;
}

.expr-apply-toggle{
  min-width: 36px;
  padding: 6px 0;
  font-size: 13px;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.expr-apply-toggle svg{
  fill: currentColor;
}

.bdd-apply-toggle{
  width: 36px;
  height: 28px;
  padding: 0;
}

.bdd-apply-toggle[hidden]{
  display: none !important;
  visibility: hidden !important;
  pointer-events: none !important;
}

.bdd-restrict-progress{
  min-width: 48px;
  height: 28px;
  padding: 0 8px;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}

.cy-wrap{
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.layer-axis{
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 80px;
  pointer-events: auto;
  z-index: 5;
  background: #fff;
  border-right: 1px solid var(--line);
  user-select: none;
}

.layer-tick{
  position: absolute;          /* Required to keep ticks aligned instead of stacking vertically */
  left: 8px;
  transform: translateY(-50%); /* Keep tick center aligned with layer center */
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
  font-size: 14px;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(255,255,255,.92);
  border: 1px solid transparent;
}

.layer-tick[data-kind="var"]{
  cursor: grab;
}

.layer-tick[data-kind="var"]:hover{
  border-color: var(--line);
  background: #f8fafc;
}

.layer-tick.dragging{
  cursor: grabbing;
  border-color: rgba(37,99,235,.35);
  background: rgba(219,234,254,.95);
  box-shadow: 0 4px 12px rgba(37,99,235,.15);
}

.layer-dot{
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: #94a3b8;
  flex: 0 0 auto;
}

.layer-label{
  font-size: 13px;
  color: var(--text);
}

#cy{
  position: absolute;
  left: 80px;
  top: 0;
  right: 0;
  bottom: 0;
}

.keyboard.kbd-panel.is-collapsed{
  transform: none;
}

.keyboard.kbd-panel.is-collapsed .kbd-body{
  display: none;
}

.var-limit-dialog{
  position: fixed;
  inset: 0;
  z-index: 1200;
}

.var-limit-dialog[hidden]{
  display: none !important;
}

.var-limit-dialog__backdrop{
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
}

.var-limit-dialog__panel{
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(440px, calc(100% - 32px));
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 20px 40px rgba(15, 23, 42, 0.18);
  padding: 16px;
}

.var-limit-dialog__title{
  margin: 0 0 8px 0;
  font-size: 16px;
  font-weight: 700;
}

.var-limit-dialog__message{
  margin: 0;
  font-size: 14px;
  color: #334155;
  line-height: 1.45;
}

.var-limit-dialog__actions{
  margin-top: 14px;
  display: flex;
  justify-content: flex-end;
}
/* Compact rows */
.keyboard.kbd-panel .kbd-row{
  display:grid;
  gap: 6px;
  grid-template-columns: repeat(auto-fit, minmax(44px, 1fr));
}

/* Slightly smaller keys to fit the red zone */
.keyboard.kbd-panel .key{
  min-height: 36px;
  padding: 8px 6px;
  font-size: 16px;
  border-radius: 10px;
}

.keyboard.kbd-panel .key-fn-template{
  font-size: 12px;
  min-height: 42px;
  padding: 10px 12px;
}

.keyboard.kbd-panel .key:not(.key-fn-template){
  width: 100%;
  max-width: 52px;
  aspect-ratio: 1 / 1;
  min-height: 0;
  padding: 0;
  justify-self: start;
}

.bdd-bar{
  flex: 0 0 auto;
  display:flex;
  align-items:center;
  gap:10px;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  background: var(--panel);
}

#btnReduceTerminals,
#btnReduceRedundant,
#btnReduceMerge,
#btnLayout{
  display:inline-flex;
  align-items:center;
  justify-content:center;
}

.bdd-bar-label{
  font-size: 12px;
  color: var(--muted);
}

.bdd-bar-label[hidden]{
  display: none !important;
}

.expr-reduce-btn[hidden],
#btnLayout[hidden]{
  display: none !important;
}

#btnLayout{
  background: #fef3c7;
  border-color: #fde68a;
  color: #7c5b00;
}

#btnLayout:hover{
  background: #fef9c3;
  border-color: #fcd34d;
}

#btnLayout:disabled{
  background: #fffbeb;
  border-color: #fef3c7;
  color: #b08917;
}

.bdd-bar-spacer{
  flex: 1 1 auto;
}

.resizer{
  width: 6px;
  flex-shrink: 0;
  cursor: col-resize;
  background: var(--line);
  border-radius: 2px;
  margin: 0 6px;
  transition: width .22s ease, margin .22s ease, opacity .18s ease;
}
.resizer:hover{ background: var(--muted); }
.resizer:active{ background: var(--accent); }

.main.left-collapsed .resizer{
  width: 0;
  margin: 0;
  opacity: 0;
  pointer-events: none;
}

.left-drawer-toggle{
  position: relative;
  width: auto;
  height: auto;
  border: none;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  color: #3e4656;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 2px 4px;
  transition: color .18s ease;
}

.left-drawer-toggle:hover{
  color:#2e3542;
}

.left-drawer-toggle__icon{
  font-size: 14px;
  line-height: 1;
  font-weight: 700;
  letter-spacing: -.08em;
  transition: transform .22s ease;
}

.left-drawer-toggle--restore{
  position: absolute;
  top: 27px;
  left: 12px;
  z-index: 8;
}

.left-drawer-toggle--restore .left-drawer-toggle__icon{
  transform: scaleX(-1);
}

.main:not(.left-collapsed) .left-drawer-toggle--restore{
  display: none;
}

.main.left-collapsed .left-actions #leftDrawerToggle{
  display: none;
}

.keyboard.kbd-panel{
  position: fixed;
  left: 12px;
  bottom: 12px;
  width: auto;
  max-width: calc(100vw - 24px);
  margin: 0;
  border: none;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  overflow: visible;
  z-index: 20;
  transition: left .2s ease, right .2s ease, bottom .2s ease, width .2s ease, max-height .2s ease;
}

.keyboard.kbd-panel:not(.is-collapsed){
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  max-width: 100vw;
  border-top: 1px solid var(--line);
  border-radius: 14px 14px 0 0;
  background: var(--panel);
  box-shadow: 0 -8px 24px rgba(15, 23, 42, 0.16);
  overflow: hidden;
}

.keyboard.kbd-panel.is-collapsed{
  right: auto;
  left: 12px;
  bottom: 12px;
  width: auto;
  max-width: none;
  border: none;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  overflow: visible;
}

.keyboard.kbd-panel .kbd-header{
  position: absolute;
  left: 10px;
  top: 8px;
  z-index: 3;
  padding: 0;
  width: auto;
  border: none;
}

.keyboard.kbd-panel:not(.is-collapsed) .kbd-header{
  justify-content: flex-start;
}

.keyboard.kbd-panel.is-collapsed .kbd-toggle{
  width: 42px;
  height: 32px;
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.keyboard.kbd-panel.is-collapsed .kbd-header{
  position: static;
  left: auto;
  top: auto;
}

.keyboard.kbd-panel .kbd-body{
  display: block;
  padding: 0;
  height: 228px;
  max-height: 228px;
  overflow: hidden;
}

.keyboard.kbd-panel.is-collapsed .kbd-body{
  display: none;
}

.keyboard.kbd-panel .kbd-area{
  padding: 0;
}

.keyboard.kbd-panel .kbd-grid{
  display: grid;
  grid-template-columns: repeat(6, 56px);
  grid-auto-rows: 39px;
  gap: 3px;
  align-items: stretch;
  justify-content: center;
  align-content: center;
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

.keyboard.kbd-panel .kbd-grid .key{
  max-width: none;
  width: 100%;
  justify-self: stretch;
}

.keyboard.kbd-panel .key-fn-template{
  min-height: 39px;
  font-size: 12px;
  padding: 6px 9px;
  justify-content: center;
  text-align: center;
}

.keyboard.kbd-panel .key-fn-grid{
  grid-column: span 2;
  aspect-ratio: auto;
  min-height: 39px;
  justify-self: stretch;
}

.keyboard.kbd-panel .kbd-placeholder{
  visibility: hidden;
  pointer-events: none;
}

@media (max-width: 980px){
  .keyboard.kbd-panel:not(.is-collapsed){
    border-radius: 12px 12px 0 0;
  }

  .keyboard.kbd-panel .kbd-body{
    height: 228px;
    max-height: 228px;
  }

  .keyboard.kbd-panel .kbd-grid{
    grid-template-columns: repeat(6, 54px);
    grid-auto-rows: 37px;
  }

}
