:root{
  --bg:#0b0e12;           /* page background */
  --panel:#11151b;        /* card/panel */
  --muted:#b8c5e0;        /* secondary text - brighter */
  --text:#ffffff;         /* main text - pure white */
  --accent:#67e8f9;       /* cyan */
  --accent-2:#22d3ee;
  --good:#34d399;         /* green */
  --warn:#f59e0b;         /* amber */
  --bad:#f87171;          /* red */
  --btn:#0f1720;
  --grid:#1b2330;
  --grid-strong:#263141;
}

[data-theme="light"]{
  --bg:#f8fafc;
  --panel:#ffffff;
  --muted:#64748b;
  --text:#1e293b;
  --accent:#0ea5e9;
  --accent-2:#0284c7;
  --good:#059669;
  --warn:#d97706;
  --bad:#dc2626;
  --btn:#f1f5f9;
  --grid:#e2e8f0;
  --grid-strong:#cbd5e1;
}

*{box-sizing:border-box}
html,body{height:100%; overflow-x:hidden}
body{
  margin:0;
  font-family:Inter, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Noto Sans", "Apple Color Emoji", "Segoe UI Emoji";
  background:var(--bg);
  color:var(--text);
  -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
}

.app{display:flex; gap:0; padding:20px; min-height:calc(100vh - 40px); position:relative; overflow-x:auto}
aside{
  width:560px; 
  min-width:320px; 
  max-width:800px; 
  background:var(--panel); 
  border-radius:20px; 
  padding:24px 24px; 
  display:flex; 
  flex-direction:column; 
  gap:20px;
  transition: width 0.1s ease;
}

/* Resize Handle */
.resize-handle {
  width: 20px;
  background: transparent;
  cursor: col-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  z-index: 10;
  transition: all 0.2s ease;
  align-self: stretch;
}

.resize-handle:hover {
  background: rgba(103, 232, 249, 0.1);
}

.resize-arrow {
  background: var(--panel);
  border: 1px solid var(--grid);
  border-radius: 12px;
  padding: 8px 4px;
  color: var(--muted);
  transition: all 0.2s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 50vh;
}

.resize-handle:hover .resize-arrow {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
  transform: scale(1.1);
}

.resize-arrow svg {
  display: block;
  transform: rotate(90deg);
}
.char-head{display:flex; align-items:center; gap:12px; margin-top:8px; padding-top:8px}
.big-char{
  font-family:"Noto Sans SC", "PingFang SC", "Hiragino Sans GB", STHeiti, system-ui, sans-serif;
  font-size:56px;
  line-height:1;
  background:var(--btn);
  border:1px solid var(--grid);
  border-radius:16px;
  width:80px;
  height:80px;
  display:grid;
  place-items:center
}
.pinyin{
  background:var(--btn);
  border:1px solid var(--grid);
  padding:8px 14px;
  border-radius:999px;
  font-size:16px;
  font-weight:600;
  color:var(--accent)
}

.stat-grid{display:grid; grid-template-columns:1fr; gap:12px}
.stat{background:var(--btn); border:1px solid var(--grid); border-radius:16px; padding:16px}
.stat .label{font-size:14px; color:var(--muted); letter-spacing:.02em}
.stat .value{font-weight:700; font-size:22px; margin-top:4px}
.btn-row{
    display: flex; 
    gap: 12px;
    margin: 12px 0;
    flex-wrap: wrap;
}
.progress-row{display:flex; justify-content:center; align-items:center; margin-top:8px}

button{
  appearance:none;
  border:1px solid var(--grid);
  background:var(--btn);
  color:var(--text);
  border-radius:12px;
  padding:12px 16px;
  font-weight:600;
  cursor:pointer;
  font-size:14px
}
button:hover{border-color:var(--grid-strong)}
button.primary{background:linear-gradient(180deg, var(--accent), var(--accent-2)); color:var(--bg); border:none}
button.ghost{
    background: var(--btn); 
    font-size: 18px; 
    padding: 16px 20px;
    border-radius: 16px;
    font-weight: 600;
    transition: all 0.2s ease;
    border: 1px solid var(--grid);
    color: var(--text);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
}

button.ghost:hover {
    background: var(--accent);
    color: var(--bg);
    border-color: var(--accent);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* Specific button styling */
#btnStroke, #btnShuffle, #hintButton {
    background: var(--btn);
    color: var(--text);
    border: 1px solid var(--grid);
    font-weight: 600;
}

#btnStroke:hover, #btnShuffle:hover, #hintButton:hover {
    background: var(--accent);
    color: var(--bg);
    border-color: var(--accent);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
button.small{padding:6px 10px; font-size:13px; border-radius:10px}

main{
  flex:1;
  background:var(--panel);
  border-radius:16px;
  padding:20px;
  display:flex;
  flex-direction:column;
  align-items:center;
  justify-content:center;
  min-height:500px;
  margin-left:20px;
  min-width:400px;
}

.board-wrap{
  position:relative;
  width:400px;
  height:400px;
  border-radius:12px;
  overflow:hidden;
  border:1px solid var(--grid);
  background:var(--bg);
  margin:0 auto 20px auto;
  transition: all 0.3s ease;
}


/* tianzige background - 4 quadrants */
.grid-bg{
  position:absolute;
  inset:0;
  background:var(--bg);
}

/* Outer border */
.grid-bg::before{
  content:"";
  position:absolute;
  top:20px; left:20px; right:20px; bottom:20px;
  border:2px solid var(--grid-strong);
  border-radius:4px;
}

/* Inner tianzige lines */
.grid-bg::after{
  content:"";
  position:absolute;
  top:20px; left:20px; right:20px; bottom:20px;
  background-image:
    /* Vertical center line */
    linear-gradient(90deg, transparent calc(50% - 1px), var(--grid) calc(50% - 1px), var(--grid) calc(50% + 1px), transparent calc(50% + 1px)),
    /* Horizontal center line */
    linear-gradient(0deg, transparent calc(50% - 1px), var(--grid) calc(50% - 1px), var(--grid) calc(50% + 1px), transparent calc(50% + 1px)),
    /* Diagonal lines */
    linear-gradient(45deg, var(--grid) 1px, transparent 1px),
    linear-gradient(-45deg, var(--grid) 1px, transparent 1px);
  background-size:
    100% 100%,
    100% 100%,
    141.42% 141.42%,
    141.42% 141.42%;
  background-position:
    center,
    center,
    center,
    center;
  background-repeat: no-repeat;
}

.global-header{
  background: var(--panel);
  border-bottom: 1px solid var(--grid);
  padding: 20px;
  position: sticky;
  top: 0;
  z-index: 100;
  margin-bottom: 0;
}

.header-content{
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: none;
  margin: 0;
  width: 100%;
}

.header-left{
  display: flex;
  align-items: center;
  gap: 24px;
  flex: 1;
}

.app-title{
  font-size: 36px;
  font-weight: 900;
  margin: 0;
  color: var(--text);
  letter-spacing: -0.5px;
}

.header-info{
  display: flex;
  gap: 15px;
  font-size: 14px;
  color: var(--muted);
}

.character-counter{
  font-weight: 500;
}

.header-right{
  display: flex;
  align-items: center;
  gap: 12px;
}

.theme-toggle{
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 0;
}

.theme-toggle:hover{
  background: var(--btn);
  color: var(--text);
  border-color: transparent;
}

.theme-toggle svg{
  transition: transform 0.3s ease;
}

.loading {
  opacity: 0.6;
  pointer-events: none;
}

.loading::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20px;
  height: 20px;
  margin: -10px 0 0 -10px;
  border: 2px solid var(--muted);
  border-top: 2px solid var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Select decks */
.deck-selector{
  background: var(--btn);
  border: 1px solid var(--grid);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  min-width: 140px;
}

.deck-selector:focus{
  outline: none;
  border-color: var(--accent);
}

[data-theme="light"] .theme-toggle svg{
  transform: rotate(180deg);
}

canvas{position:absolute; inset:0; width:100%; height:100%; touch-action:none}
#bgLayer{z-index:1}
#drawLayer{z-index:2}

.toolbar{
  display:flex;
  gap:12px;
  align-items:center;
  justify-content:center;
  width:fit-content;
  margin-top:20px;
  padding:16px;
  background:var(--panel);
  border:1px solid var(--grid);
  border-radius:16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.left-tools, .right-tools{display:flex; gap:12px; align-items:center}

.toolbar button{
  background: var(--btn);
  border: 1px solid var(--grid);
  color: var(--text);
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  width: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex-shrink: 0;
}

.toolbar button:hover {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
.status{font-weight:700}

.hint-pill{font-size:12px; color:var(--muted)}
.kbd{
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  background:var(--btn);
  border:1px solid var(--grid);
  padding:2px 6px;
  border-radius:6px;
  font-size:12px;
  color:var(--text)
}

/* iPad and larger tablets in landscape */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
  .app {
    padding: 16px;
    gap: 16px;
  }
  
  aside {
    width: 400px;
    min-width: 350px;
  }
  
  .board-wrap {
    width: 500px;
    height: 500px;
  }
  
  .toolbar button {
    padding: 14px 18px;
    font-size: 16px;
    min-height: 44px; /* Apple's recommended touch target size */
  }
  
  .char-head .big-char {
    font-size: 64px;
    width: 90px;
    height: 90px;
  }
  
  .btn-row button {
    padding: 18px 22px;
    font-size: 18px;
    min-height: 44px;
  }
}

/* iPad and larger tablets in portrait */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: portrait) {
  .app {
    flex-direction: column;
    padding: 20px;
    gap: 20px;
    min-height: auto;
    overflow-y: auto;
  }
  
  body {
    overflow-y: auto;
  }
  
  aside {
    width: auto;
    max-width: none;
  }
  
  .board-wrap {
    width: min(600px, 90vw);
    height: min(600px, 90vw);
    margin: 0 auto 20px auto;
  }
  
  .toolbar {
    flex-wrap: wrap;
    gap: 12px;
    padding: 20px;
  }
  
  .toolbar button {
    padding: 16px 20px;
    font-size: 18px;
    min-height: 48px;
    min-width: 120px;
  }
  
  .char-head {
    justify-content: center;
    text-align: center;
  }
  
  .char-head .big-char {
    font-size: 72px;
    width: 100px;
    height: 100px;
  }
  
  .btn-row {
    justify-content: center;
  }
  
  .btn-row button {
    padding: 20px 24px;
    font-size: 20px;
    min-height: 48px;
  }
  
  #slider-container {
    padding: 16px;
    gap: 16px;
  }
  
  #brushSelector img {
    width: 44px;
    height: 44px;
    padding: 6px;
  }
  
  #lineWidthSlider {
    height: 12px;
    min-width: 120px;
  }
  
  #lineWidthSlider::-webkit-slider-thumb {
    width: 24px;
    height: 24px;
  }
  
  #lineWidthSlider::-moz-range-thumb {
    width: 24px;
    height: 24px;
  }
}

/* Mobile phones and small tablets */
@media (max-width: 767px) {
  .app {
    flex-direction: column;
    padding: 12px;
    gap: 12px;
    min-height: auto;
    overflow-y: auto;
  }
  
  body {
    overflow-y: auto;
  }
  
  aside {
    width: auto;
  }
  
  main {
    padding: 12px;
  }
  
  .board-wrap {
    width: min(400px, 100vw - 24px);
    height: min(400px, 100vw - 24px);
  }
  
  .toolbar {
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px;
  }
  
  .toolbar button {
    min-height: 44px;
    padding: 12px 16px;
  }
  
  .char-head {
    margin-top: 16px;
    padding-top: 16px;
  }
  
  .char-head .big-char {
    font-size: 48px;
    width: 70px;
    height: 70px;
  }
  
  .btn-row button {
    padding: 14px 18px;
    font-size: 16px;
    min-height: 44px;
  }
}


/* Brush and Size Control Panel */
#slider-container {
    background: var(--panel);
    border: 1px solid var(--grid);
    border-radius: 16px;
    padding: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);
    transition: all 0.3s ease;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    margin: 8px 0;
    width: 100%;
    max-width: 100%;
}

#slider-container:hover {
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
    transform: translateY(-1px);
}

#brushSelector {
    display: flex;
    gap: 3px;
    background: var(--panel);
    border: 1px solid var(--grid);
    border-radius: 8px;
    padding: 3px;
    transition: all 0.3s ease;
    flex-shrink: 0;
}

.slider-group {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    min-width: 140px;
}

#brushSelector:hover {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    transform: translateY(-1px);
}

#brushSelector img {
    width: 36px;
    height: 36px;
    cursor: pointer;
    border: 2px solid transparent;
    border-radius: 10px;
    padding: 4px;
    background: transparent;
    transition: all 0.2s ease;
    position: relative;
    overflow: hidden;
}

#brushSelector img:hover {
    border-color: var(--accent);
}

#brushSelector img.selected {
    border-color: var(--accent);
    background: transparent;
    box-shadow: 0 0 0 2px var(--accent);
    transform: scale(1.05);
}

#brushSelector img.selected::after {
    content: '✓';
    position: absolute;
    top: 1px;
    right: 1px;
    background: var(--text);
    color: var(--accent);
    border-radius: 50%;
    width: 12px;
    height: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 8px;
    font-weight: bold;
}

/* Dark mode brush selector styling */
body:not([data-theme="light"]) #brushSelector img {
    filter: invert(1);
    background: transparent !important;
}

body:not([data-theme="light"]) #brushSelector img.selected {
    filter: invert(1) hue-rotate(180deg);
    background: transparent !important;
}

body:not([data-theme="light"]) #brushSelector img:hover {
    border-color: var(--accent);
}

/* Add subtle animation for brush selection */
@keyframes brushSelect {
    0% { transform: scale(1); }
    50% { transform: scale(1.15); }
    100% { transform: scale(1.05); }
}

#brushSelector img.selected {
    animation: brushSelect 0.3s ease;
}

/* Size Slider Styling */
#lineWidthSlider {
    width: 100%;
    min-width: 80px;
    flex: 1;
    height: 10px;
    border-radius: 5px;
    background: var(--grid);
    outline: none;
    -webkit-appearance: none;
    appearance: none;
    cursor: pointer;
    transition: all 0.2s ease;
}

#lineWidthSlider:hover {
    background: var(--grid-strong);
}

#lineWidthSlider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: 2px solid var(--panel);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    transition: all 0.2s ease;
}

#lineWidthSlider::-webkit-slider-thumb:hover {
    transform: scale(1.1);
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.3);
}

#lineWidthSlider::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: 2px solid var(--panel);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    transition: all 0.2s ease;
}

#lineWidthSlider::-moz-range-thumb:hover {
    transform: scale(1.1);
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.3);
}

/* Size Label */
label[for="lineWidthSlider"] {
    display: flex;
    align-items: center;
    font-size: 11px;
    color: var(--muted);
    font-weight: 500;
    white-space: nowrap;
}

#lineWidthValue {
    background: var(--accent);
    color: var(--bg);
    padding: 2px 8px;
    border-radius: 12px;
    font-weight: 600;
    font-size: 11px;
    min-width: 24px;
    text-align: center;
}


/* =========================================
   Mobile & Tablet Touch Styles
   ========================================= */
canvas#drawLayer {
  /* This tells the browser to let our JavaScript
     handle all touch actions on the canvas. */
  touch-action: none;
}

body, aside {
  -webkit-touch-callout: none;

  /* These rules prevent text from being highlighted when you 
     accidentally drag outside the canvas. */
  -webkit-user-select: none; /* For Safari/Chrome */
  -moz-user-select: none;    /* For Firefox */
  -ms-user-select: none;     /* For IE/Edge */
  user-select: none;         /* Standard */
}

/* Safari/iPad Pencil hygiene */
canvas { -webkit-touch-callout:none; -webkit-user-select:none; user-select:none; }
/* Allow scrolling on body, but disable touch-action only on canvas */
html { touch-action: manipulation; }
body { touch-action: manipulation; }

/* iPad-specific enhancements */
@media (min-width: 768px) and (max-width: 1024px) {
  /* Improve modal sizing for iPad */
  .modal-content {
    width: 85%;
    max-width: 700px;
    height: 75%;
    max-height: 500px;
  }
  
  .char-grid {
    grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
    gap: 12px;
  }
  
  .char-grid-item {
    font-size: 1.8rem;
    min-height: 70px;
  }
  
  /* Better header for iPad */
  .global-header {
    padding: 24px;
  }
  
  .app-title {
    font-size: 42px;
  }
  
  .deck-selector {
    padding: 12px 18px;
    font-size: 18px;
    min-width: 160px;
  }
  
  .theme-toggle {
    padding: 12px;
  }
  
  .theme-toggle svg {
    width: 24px;
    height: 24px;
  }
  
  /* Improve resize handle for touch */
  .resize-handle {
    width: 24px;
  }
  
  .resize-arrow {
    padding: 12px 6px;
  }
  
  /* Better stat display */
  .stat {
    padding: 20px;
  }
  
  .stat .label {
    font-size: 16px;
  }
  
  .stat .value {
    font-size: 26px;
  }
  
  .pinyin {
    padding: 10px 16px;
    font-size: 18px;
  }
}

/* =========================================
   Character Grid Modal Styles
   ========================================= */
.char-modal {
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 1;
  transition: opacity 0.2s ease-in-out;
}

.char-modal.hidden {
  opacity: 0;
  pointer-events: none;
}

.modal-content {
  background: var(--panel);
  padding: 24px;
  border: 1px solid var(--grid-strong);
  border-radius: 12px;
  width: 90%;
  max-width: 800px;
  height: 80%;
  max-height: 600px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-shrink: 0;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.2rem;
  color: var(--text);
}

.modal-close-btn {
  background: none;
  border: none;
  font-size: 2rem;
  font-weight: 300;
  color: var(--muted);
  cursor: pointer;
  line-height: 1;
  padding: 0 8px;
  transition: color 0.2s;
}

.modal-close-btn:hover {
  color: var(--text);
}

.char-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(60px, 1fr));
  gap: 10px;
  overflow-y: auto;
  padding-right: 10px;
}

.char-grid-item {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--btn);
  border: 1px solid var(--grid);
  border-radius: 8px;
  font-family: "Noto Sans SC", "PingFang SC", "Hiragino Sans GB", STHeiti, system-ui, sans-serif;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
  aspect-ratio: 1 / 1;
  cursor: pointer;
  transition: border-color 0.2s, background-color 0.2s;
  padding: 4px;
}

.char-grid-item:hover {
  border-color: var(--accent);
  background: var(--grid);
}

.char-grid-item.current {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--bg);
  box-shadow: 0 0 0 3px var(--accent), 0 0 20px rgba(103, 232, 249, 0.3);
  transform: scale(1.05);
  z-index: 10;
  position: relative;
}

.char-grid-item.current:hover {
  background: var(--accent-2);
  border-color: var(--accent-2);
  box-shadow: 0 0 0 3px var(--accent-2), 0 0 20px rgba(103, 232, 249, 0.5);
}

.char-grid-item img {
  border-radius: 4px;
}