:root {
  --ink: #30265f;
  --muted: #817a9c;
  --violet: #7862d3;
  --line: #e7e0f8;
  --panel-shadow: 0 8px 25px rgba(48, 38, 95, .14);
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body { color: var(--ink); background: #fff; }

/* Keep the work surface full screen; the brand mark and tool rail float over it. */
#viskas {
  position: relative;
  display: flex;
  width: 100%;
  max-width: none;
  height: 100vh;
  height: 100dvh;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
#learning-header {
  position: absolute;
  z-index: 20;
  top: 12px;
  left: 12px;
  display: grid;
  width: 42px;
  height: 42px;
  min-height: 0;
  padding: 2px;
  place-items: center;
  border: 1px solid rgba(227,221,244,.9);
  border-radius: 14px;
  background: rgba(255,255,255,.94);
  box-shadow: 0 4px 14px rgba(48,38,95,.12);
  pointer-events: none;
}
#learning-header > div, .learning-spark { display: none; }
.learning-mascot {
  display: grid;
  width: 36px;
  height: 36px;
  place-items: center;
  border: 1px solid #ffe3a3;
  border-radius: 11px;
  background: #fff2ce;
  font-size: 22px;
}

#toolbar {
  position: absolute;
  z-index: 21;
  top: 12px;
  left: 50%;
  display: flex;
  width: max-content;
  max-width: calc(100vw - 150px);
  min-width: 0;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 5px 7px;
  overflow-x: auto;
  overflow-y: hidden;
  border: 1px solid rgba(229,225,239,.94);
  border-radius: 15px;
  background: rgba(255,255,255,.94);
  box-shadow: 0 5px 17px rgba(48,38,95,.12);
  backdrop-filter: blur(16px) saturate(1.2);
  transform: translateX(-50%);
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}
#toolbar > .tool-section { display: flex; flex: 0 0 auto; align-items: center; gap: 4px; padding: 0; }
#toolbar > .toolbar-separator { flex: 0 0 1px; width: 1px; height: 25px; margin: 0 2px; background: #ece9f2; }
#toolbar .section-header, #toolbar .tool-label { display: none; }
#toolbar .tool-grid { display: flex; align-items: center; gap: 3px; }
#toolbar .tool-wrapper { display: flex; flex: 0 0 auto; align-items: center; justify-content: center; gap: 0; }
#toolbar .tool-button {
  display: inline-flex;
  width: 36px;
  height: 36px;
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 10px;
  color: #655b83;
  background: #f7f6fb;
  cursor: pointer;
  transition: transform .15s ease, background .15s ease, box-shadow .15s ease;
  touch-action: manipulation;
}
#toolbar .tool-button svg, #tablet-toolbar .tool-button svg { display: block; width: 22px; height: 22px; }
#toolbar .tool-button:hover { transform: translateY(-1px); box-shadow: 0 3px 8px rgba(48,38,95,.15); }
#toolbar .tool-button:focus-visible, #answer-close:focus-visible, #answer-sheet-close:focus-visible { outline: 3px solid #b9aaff; outline-offset: 2px; }
#toolbar .tool-button.tool-active { border-color: #7966d0; background: linear-gradient(145deg,#8c77e9,#705bc9) !important; box-shadow: 0 3px 10px rgba(108,83,199,.28); }
#toolbar .tool-wrapper:has(#recognizeTool) .tool-button { background: #eaf6ff; }
#toolbar .tool-wrapper:has(#solveTool) .tool-button { background: #fff5d7; }
#toolbar .tool-wrapper:has(#solution-toggle) .tool-button { background: #f1edff; }
#toolbar .tool-wrapper:has(#solveTool) .tool-button.glowing-ai-button { animation: solve-pulse 2s ease-in-out infinite; }
@keyframes solve-pulse { 50% { box-shadow: 0 0 0 3px rgba(242,193,69,.2), 0 4px 12px rgba(230,169,44,.2); } }

#colorPalette { display: flex; flex: 0 0 auto; align-items: center; gap: 4px; }
.color-button { width: 20px; height: 20px; flex: 0 0 auto; border: 2px solid #fff; border-radius: 50%; cursor: pointer; box-shadow: 0 1px 4px rgba(37,31,62,.18); transition: transform .15s ease, outline .15s ease; }
.color-button:hover { transform: scale(1.12); }
.color-button.active { outline: 2px solid #7966d0; outline-offset: 1px; }
#white { border-color: #dedbe7; }
#sizePreview { display: none; }
#sizeSlider { width: 66px; height: 24px; margin: 0; accent-color: #7862d3; }
#sizeValue { min-width: 14px; color: #817a9c; font-size: .66rem; text-align: center; }
#toolbar #lang-switcher, #toolbar #lang-switcher-tablet {
  display: inline-flex;
  width: 50px;
  height: 34px;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 0 5px;
  border: 1px solid #e2dcf4;
  border-radius: 10px;
  color: #6551b2;
  background: #f5f2ff;
  font-size: .68rem;
  font-weight: 800;
}
#toolbar #lang-switcher::before, #toolbar #lang-switcher-tablet::before { content: "🌐"; font-size: 13px; }

#main-content { position: absolute; inset: 0; display: block; min-width: 0; min-height: 0; overflow: hidden; }
#canvas-container {
  position: absolute;
  inset: 0;
  min-width: 0;
  border: 0;
  border-radius: 0;
  background-color: #fff;
  background-image: radial-gradient(#e5e1f2 .8px, transparent .8px);
  background-size: 28px 28px;
  box-shadow: none;
}
canvas { background: transparent; touch-action: none; }

/* The solution is a drawer so it never permanently takes space from the board. */
#answer-container {
  position: absolute;
  z-index: 18;
  top: 64px;
  right: 12px;
  bottom: 12px;
  display: flex;
  width: clamp(300px, 27vw, 390px);
  min-width: 0;
  padding: 15px;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid #e5def8;
  border-radius: 20px;
  background: rgba(255,255,255,.97);
  box-shadow: var(--panel-shadow);
  opacity: 0;
  pointer-events: none;
  transform: translateX(calc(100% + 24px));
  transition: transform .22s ease, opacity .22s ease;
}
#answer-container.is-open { opacity: 1; pointer-events: auto; transform: translateX(0); }
.answer-heading { display: flex; flex: 0 0 auto; align-items: center; justify-content: space-between; gap: 8px; }
#answer-container h3 { min-width: 0; margin: 0; padding: 0; border: 0; color: #40346e; font-size: .96rem; font-weight: 800; }
#answer-close { display: grid; width: 32px; height: 32px; flex: 0 0 auto; place-items: center; border: 0; border-radius: 9px; background: #f3efff; color: #574694; font-size: 1.25rem; cursor: pointer; }
#answer-content { flex: 1 1 auto; min-height: 0; margin-top: 10px; overflow-y: auto; color: #514879; line-height: 1.6; overscroll-behavior: contain; }
#answer-content [style*="color: #888"] { display: inline-block; padding: 10px 12px; border: 1px dashed #d9d0f1; border-radius: 12px; background: #f6f3ff; color: #81789f !important; }
#answer-sheet-backdrop { position: fixed; z-index: 30; inset: 0; background: rgba(35,28,66,.42); opacity: 0; pointer-events: none; transition: opacity .2s ease; backdrop-filter: blur(3px); }
#answer-sheet-backdrop.is-open { opacity: 1; pointer-events: auto; }
#answer-sheet { display: none; position: fixed; z-index: 31; right: 0; bottom: 0; left: 0; border: 1px solid #e5def8; border-bottom: 0; border-radius: 20px 20px 0 0; background: #fff; box-shadow: 0 -8px 28px rgba(48,38,95,.16); transform: translateY(105%); visibility: hidden; pointer-events: none; transition: transform .22s ease, visibility .22s ease; }
#answer-sheet.is-open { display: block; transform: translateY(0); visibility: visible; pointer-events: auto; }
#answer-sheet-content { display: flex; max-height: 68dvh; padding: 14px 16px calc(14px + env(safe-area-inset-bottom)); flex-direction: column; }
#answer-sheet-header { display: flex; flex: 0 0 auto; align-items: center; justify-content: space-between; }
#answer-sheet-header h3 { margin: 0; color: #40346e; font-size: 1rem; }
#answer-sheet-close { display: grid; width: 38px; height: 38px; place-items: center; border: 0; border-radius: 11px; background: #f3efff; color: #574694; font-size: 1.4rem; cursor: pointer; }
#answer-sheet-content-inner { min-height: 0; margin-top: 8px; overflow-y: auto; color: #514879; line-height: 1.6; overscroll-behavior: contain; }
#recognition-toast { position: fixed; z-index: 35; top: 64px; right: 14px; max-width: min(390px, calc(100vw - 28px)); padding: 14px; border: 1px solid #e5def8; border-radius: 16px; background: #fff; color: #40346e; box-shadow: var(--panel-shadow); opacity: 0; pointer-events: none; transform: translateY(-8px); transition: opacity .2s ease, transform .2s ease; }
#recognition-toast.active { opacity: 1; pointer-events: auto; transform: translateY(0); }
#recognition-toast h3 { margin: 0; padding-bottom: 7px; border-bottom: 1px solid #eee9f8; font-size: .86rem; }
#recognition-content { margin-top: 7px; font-size: 1.3rem; }

#ai-usage-widget { position: fixed; z-index: 22; bottom: 12px; left: 12px; }
#ai-usage-toggle {
  display: inline-flex;
  min-height: 34px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 11px 0 7px;
  border: 1px solid #e4dfef;
  border-radius: 12px;
  background: rgba(255,255,255,.94);
  color: #514879;
  box-shadow: 0 4px 14px rgba(48,38,95,.12);
  font: 650 .72rem/1 system-ui, sans-serif;
  cursor: pointer;
  backdrop-filter: blur(12px);
}
.ai-usage-glyph { display: grid; width: 22px; height: 22px; place-items: center; border-radius: 7px; background: #e8f7ef; color: #218b5f; font-size: 1rem; }
#ai-usage-toggle.is-limited .ai-usage-glyph { background: #fff0e8; color: #c26045; }
#ai-usage-panel[hidden] { display: none !important; }
#ai-usage-panel {
  position: absolute;
  bottom: 42px;
  left: 0;
  width: min(310px, calc(100vw - 24px));
  padding: 14px;
  border: 1px solid #e4def1;
  border-radius: 16px;
  background: rgba(255,255,255,.98);
  color: #514879;
  box-shadow: 0 12px 32px rgba(48,38,95,.18);
  font: 400 .76rem/1.4 system-ui, sans-serif;
}
.ai-usage-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 9px; color: #362b66; font-size: .88rem; }
#ai-usage-close { width: 27px; height: 27px; border: 0; border-radius: 8px; background: #f2effa; color: #655b83; font-size: 1.1rem; cursor: pointer; }
.ai-usage-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-top: 8px; }
.ai-usage-row span { color: #77708d; }
.ai-usage-row strong { white-space: nowrap; color: #382f65; font-variant-numeric: tabular-nums; }
.ai-usage-reset { min-height: 1em; margin-top: 1px; color: #9189a6; font-size: .66rem; text-align: right; }
.ai-usage-estimate { display: grid; gap: 3px; margin-top: 10px; padding: 9px; border-radius: 11px; background: #f7f5fc; color: #6d6684; font-variant-numeric: tabular-nums; }
.ai-usage-estimate strong { color: #514879; }
#quota-retry { margin: 8px 0 0; color: #b14e39; font-weight: 700; }
#quota-retry[hidden] { display: none; }
#ai-usage-panel small { display: block; margin-top: 9px; color: #89829d; font-size: .64rem; }
#ai-usage-panel a { display: inline-block; margin-top: 7px; color: #6551b2; font-size: .68rem; text-decoration: none; }
#ai-usage-panel a:hover { text-decoration: underline; }

@media (max-width: 1050px) {
  #toolbar { max-width: calc(100vw - 120px); }
  #toolbar .tool-button { width: 33px; height: 33px; border-radius: 9px; }
  #toolbar .tool-button svg { width: 20px; height: 20px; }
  #colorPalette { gap: 3px; }
  .color-button { width: 17px; height: 17px; }
  #sizeSlider { width: 54px; }
}

@media (max-width: 700px) and (orientation: portrait) {
  #viskas { height: 100dvh; }
  #learning-header { top: max(8px, env(safe-area-inset-top)); left: max(8px, env(safe-area-inset-left)); width: 38px; height: 38px; border-radius: 12px; }
  .learning-mascot { width: 32px; height: 32px; border-radius: 9px; font-size: 19px; }
  #toolbar {
    top: max(8px, env(safe-area-inset-top));
    right: max(8px, env(safe-area-inset-right));
    left: 54px;
    width: auto;
    max-width: none;
    max-height: calc(100dvh - 24px);
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 3px 5px;
    padding: 4px 5px;
    border-radius: 13px;
    transform: none;
  }
  #toolbar > .toolbar-separator { display: none; }
  #toolbar > .tool-section { min-width: 0; gap: 3px; }
  #toolbar > .tool-section:nth-child(1) { order: 1; flex: 1 1 100%; }
  #toolbar > .tool-section:nth-child(3) { order: 2; flex: 1 1 100%; }
  #toolbar > .tool-section:nth-child(5) { order: 3; }
  #toolbar > .tool-section:nth-child(7) { order: 4; }
  #toolbar > .tool-section:nth-child(9) { order: 5; }
  #toolbar > .tool-section:nth-child(10) { order: 6; }
  #toolbar .desktop-only { display: flex; }
  #toolbar .section-header, #toolbar .tool-label { display: none; }
  #toolbar .tool-grid { gap: 2px; }
  #toolbar .tool-button { width: 30px; height: 30px; border-radius: 8px; }
  #toolbar .tool-button svg { width: 19px; height: 19px; }
  #colorPalette { gap: 2px; }
  .color-button { width: 16px; height: 16px; }
  #sizeSlider { width: clamp(42px, 13vw, 58px); height: 22px; }
  #sizeValue { min-width: 11px; font-size: .58rem; }
  #toolbar #lang-switcher, #toolbar #lang-switcher-tablet { width: 42px; height: 29px; gap: 2px; padding: 0 3px; border-radius: 8px; font-size: .6rem; }
  #toolbar #lang-switcher::before, #toolbar #lang-switcher-tablet::before { font-size: 11px; }
  #answer-container { display: none; }
  #answer-sheet { display: block; }
  #answer-sheet-content { max-height: 66dvh; }
  #recognition-toast { top: calc(52px + env(safe-area-inset-top)); right: 8px; left: 8px; max-width: none; }
  #ai-usage-widget { bottom: max(8px, env(safe-area-inset-bottom)); left: max(8px, env(safe-area-inset-left)); }
  #ai-usage-toggle { width: 34px; min-height: 34px; gap: 0; padding: 3px; border-radius: 11px; }
  #ai-usage-summary { display: none; }
  .ai-usage-glyph { width: 25px; height: 25px; font-size: 1.1rem; }
  #ai-usage-panel { bottom: 42px; left: 0; width: min(310px, calc(100vw - 16px)); }
}

@media (max-width: 390px) and (orientation: portrait) {
  #toolbar { left: 50px; gap: 2px 3px; }
  #toolbar .tool-button { width: 28px; height: 28px; }
  #toolbar .tool-button svg { width: 18px; height: 18px; }
  .color-button { width: 14px; height: 14px; }
}

@media (max-width: 850px) and (orientation: landscape) {
  #learning-header { top: 7px; left: 8px; width: 34px; height: 34px; }
  .learning-mascot { width: 28px; height: 28px; font-size: 17px; }
  #toolbar { top: 7px; max-width: calc(100vw - 95px); }
  #answer-container { top: 54px; right: 8px; bottom: 8px; width: min(340px, 42vw); }
}

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