:root {
  --primary-color: #3498db;
  --secondary-color: #2c3e50;
  --bg-light: #f0f4f8;
  --white: #ffffff;
  --accent-color: #8e1f13;
  --text-color: #333333;
  --dark-mode-bg: #121212;
  --bg: var(--bg-light);
  --text: var(--text-color);
}

:root[data-theme="dark"] {
  --bg: var(--dark-mode-bg);
  --text: #e9e9e9;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: var(--dark-mode-bg);
    --text: #e9e9e9;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: Vazir, IRANSans, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background-color: var(--bg);
  color: var(--text);
  direction: rtl;
  touch-action: manipulation;
  overflow-x: hidden;
  padding: 10px;
  transition: background-color 0.3s ease, color 0.3s ease;
}

body.night-mode {
  background-color: var(--dark-mode-bg);
  color: #e9e9e9;
  color-scheme: dark;
  transition: background-color 0.25s ease, color 0.25s ease;
}

.container {
  width: min(100%, 500px);
  margin-inline: auto;
  display: flex;
  flex-direction: column;
  min-height: 100svh;
  padding: max(10px, env(safe-area-inset-top)) 10px max(10px, env(safe-area-inset-bottom));
  gap: 12px;
  isolation: isolate;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  font-size: 18px;
  font-weight: 800;
  color: var(--primary-color);
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, #fff 75%, transparent);
  backdrop-filter: blur(8px);
  border-radius: 14px 14px 0 0;
  box-shadow: 0 6px 24px rgba(15, 23, 42, .08);
}

.header :is(h1, h2, h3, .title) {
  margin: 0 auto;
  font-weight: 900;
  letter-spacing: -0.2px;
}

.header span {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.flashcard-frame {
  width: 100%;
  min-height: 212px;
  height: auto;
  overflow-y: auto;
  overflow-x: hidden;
  background-color: var(--white);
  border: 2px solid rgba(0,0,0,.25);
  border-radius: var(--radius-lg) var(--radius-lg) var(--radius) var(--radius);
  box-shadow: var(--shadow-sm);
  margin-bottom: 12px;
  display: block;
  transition: background-color var(--transition-med), border-color var(--transition-med);
}

.night-mode .flashcard-frame {
  border-color: rgba(255,255,255,.15);
  background-color: hsl(220 15% 10%);
}

.flashcard-frame:hover {
  background-color: hsl(0 0% 98%);
}

.flashcard-container {
  width: 100%;
  height: 705px;
  position: relative;
  background-color: var(--white);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.06);
  transition: background-color var(--transition-med);
}

.night-mode .flashcard-container {
  background-color: var(--white);
}

.flashcard-container img {
  display: block;
  max-width: none;
  height: auto;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: auto;
}

.toolbar-btn {
  background: var(--white);
  border: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 5px rgba(0,0,0,.2);
  margin-left: 5px;
}

.night-mode .toolbar-btn {
  background-color: #2d2d2d;
  color: #fff;
}

.toolbox {
  display: flex;
  justify-content: space-around;
  background-color: var(--white);
  border-radius: 0 0 10px 10px;
  padding: 10px;
  border-top: 1px solid #ddd;
}

.night-mode .toolbox {
  background-color: #2d2d2d;
  border-color: #444;
}

.toolbox button {
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  color: var(--secondary-color);
  font-size: 12px;
  cursor: pointer;
  padding: 5px;
  font-family: Vazir, Arial, sans-serif;
}

.night-mode .toolbox button {
  color: #ccc;
}

.toolbox button.active {
  color: var(--primary-color);
}

.delete-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  background: var(--white);
  border: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 5px rgba(0,0,0,.2);
  z-index: 1001;
  font-family: Vazir, Arial, sans-serif;
}

.night-mode .delete-btn {
  background-color: #2d2d2d;
  color: #fff;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-menu {
  display: none;
  position: absolute;
  top: 40px;
  right: 0;
  background: var(--white);
  border: 1px solid #ddd;
  border-radius: 10px;
  box-shadow: 0px 4px 12px rgba(0,0,0,0.12);
  min-width: 160px;
  z-index: 1000;
  overflow: hidden;
}

.dropdown-menu button {
  width: 100%;
  padding: 10px 12px;
  text-align: right;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-color);
  transition: background 0.2s ease;
  font-family: Vazir, Arial, sans-serif;
}

.dropdown-menu button:hover {
  background: #f5f5f5;
}

.night-mode .dropdown-menu {
  background: #2d2d2d;
  border-color: #444;
}

.night-mode .dropdown-menu button {
  color: #e9e9e9;
}

.night-mode .dropdown-menu button:hover {
  background: #3a3a3a;
}

.tab-content {
  background-color: var(--white);
  border-radius: 12px;
  box-shadow: 0 -2px 10px rgba(0,0,0,.1);
  padding: 15px;
  margin-top: 1px;
  display: none;
  max-height: 40vh;
  overflow-y: auto;
}

.night-mode .tab-content {
  background-color: #2d2d2d;
  color: #fff;
}

.tab-content.active {
  display: block;
}

.tab-content input,
.tab-content select,
.tab-content textarea {
  resize: none;
  width: 100%;
  padding: 10px;
  margin-bottom: 10px;
  border: 1px solid var(--secondary-color);
  border-radius: 6px;
  font-family: Vazir, Arial, sans-serif;
}

.night-mode .tab-content input,
.night-mode .tab-content select,
.night-mode .tab-content textarea {
  resize: none;
  background-color: #1e1e1e;
  color: #fff;
  border-color: #444;
}

.tab-content button {
  width: 100%;
  padding: 10px;
  background-color: var(--primary-color);
  color: var(--white);
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color .3s;
  font-family: Vazir, Arial, sans-serif;
  margin-bottom: 10px;
}

.tab-content button:hover {
  background-color: var(--accent-color);
}

.export-options {
  display: flex;
  gap: 10px;
  margin-bottom: 15px;
}

.export-options select {
  flex: 1;
}

.color-picker-container {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 10px;
}

.color-preview {
  width: 40px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid #ddd;
}

.size-controls {
  display: flex;
  gap: 5px;
  margin-bottom: 10px;
}

.size-controls button {
  flex: 1;
}

input[type="range"] {
  width: 100%;
  margin-bottom: 15px;
}

.text-controls {
  display: flex;
  gap: 5px;
  margin-bottom: 10px;
}

.text-controls button {
  flex: 1;
}

.rotate-controls {
  display: flex;
  gap: 5px;
  margin-bottom: 10px;
}

.rotate-controls button {
  flex: 1;
}

.font-size-controls {
  display: flex;
  gap: 5px;
  margin-bottom: 10px;
}

.font-size-controls button {
  padding: 5px;
}

.font-size-input {
  width: 100%;
  padding: 10px;
  text-align: center;
  font-size: 16px;
}

.color-options {
  display: flex;
  gap: 5px;
}

.color-option {
  width: 25px;
  height: 25px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
}

.color-option.selected {
  border-color: var(--secondary-color);
}

.options-section {
  margin-top: 15px;
  padding-top: 15px;
  border-top: 1px dashed #ddd;
}

.night-mode .options-section {
  border-color: #444;
}

.options-section h4 {
  margin-bottom: 10px;
  color: var(--secondary-color);
}

.night-mode .options-section h4 {
  color: #ccc;
}

.hollow-checkbox {
  margin: 10px 0;
  display: flex;
  align-items: center;
}

.hollow-checkbox input {
  margin-left: 8px;
}

.underline {
  text-decoration: underline;
}

.strikethrough {
  text-decoration: line-through;
}

.text-underline {
  border-bottom: 1px solid currentColor;
  padding-bottom: 1px;
}

.element {
  position: absolute;
  cursor: move;
  user-select: none;
  touch-action: auto;
  transition: transform .1s;
  resize: both;
  overflow: hidden;
  min-width: 50px;
  min-height: 20px;
  border: none;
}

.element:active {
  transform: scale(1.05);
}

.element.selected {
  outline: 3px solid #fff;
  box-shadow: 0 0 0 3px var(--primary-color);
}

.element[data-type="text"] {
  resize: none;
  overflow: visible;
}

.font-arial      { font-family: Arial, sans-serif; direction: ltr; }
.font-times      { font-family: 'Times New Roman', serif; direction: ltr; }
.font-tahoma     { font-family: Tahoma, sans-serif; direction: ltr; }
.font-segoe      { font-family: 'Segoe UI', sans-serif; direction: ltr; }
.font-roboto     { font-family: 'Roboto', sans-serif; direction: ltr; }
.font-vazir      { font-family: 'Vazir', sans-serif; direction: rtl; }
.font-shabnam    { font-family: 'Shabnam', sans-serif; direction: rtl; }
.font-opensans   { font-family: 'Open Sans', sans-serif; direction: ltr; }
.font-lato       { font-family: 'Lato', sans-serif; direction: ltr; }
.font-sahel      { font-family: 'Sahel', sans-serif; direction: rtl; }
.font-traffic    { font-family: 'Traffic', sans-serif; direction: rtl; }
.font-nastaliq   { font-family: 'Digital Nastaliq', sans-serif; direction: rtl; }
.font-b-nazanin  { font-family: 'B Nazanin', sans-serif; direction: rtl; }
.font-b-titr     { font-family: 'B Titr', sans-serif; direction: rtl; }

#font-family option[value="font-vazir"]     { font-family: 'Vazir', sans-serif; }
#font-family option[value="font-shabnam"]   { font-family: 'Shabnam', sans-serif; }
#font-family option[value="font-sahel"]     { font-family: 'Sahel', sans-serif; }
#font-family option[value="font-traffic"]   { font-family: 'Traffic', sans-serif; }
#font-family option[value="font-nastaliq"]  { font-family: 'Digital Nastaliq', sans-serif; }
#font-family option[value="font-b-nazanin"] { font-family: 'B Nazanin', sans-serif; }
#font-family option[value="font-b-titr"]    { font-family: 'B Titr', sans-serif; }
#font-family option[value="font-arial"]     { font-family: Arial, sans-serif; }
#font-family option[value="font-times"]     { font-family: 'Times New Roman', serif; }
#font-family option[value="font-tahoma"]    { font-family: Tahoma, sans-serif; }
#font-family option[value="font-segoe"]     { font-family: 'Segoe UI', sans-serif; }
#font-family option[value="font-roboto"]    { font-family: 'Roboto', sans-serif; }
#font-family option[value="font-opensans"]  { font-family: 'Open Sans', sans-serif; }
#font-family option[value="font-lato"]      { font-family: 'Lato', sans-serif; }

#font-family {
  font-size: 16px;
  padding: 5px;
  direction: rtl;
}

.modal {
  display: none;
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background-color: rgba(0,0,0,.7);
  z-index: 2000;
  justify-content: center;
  align-items: center;
}

.modal-content {
  background-color: #fff;
  padding: 20px;
  border-radius: 10px;
  max-width: 80%;
  max-height: 80%;
  overflow-y: auto;
}

.modal.is-open {
  display: flex;
}

body.modal-open {
  overflow: hidden;
}

.night-mode .modal-content {
  background-color: #2d2d2d;
  color: #fff;
}

.close-modal {
  float: left;
  cursor: pointer;
  font-size: 20px;
}

#save-modal {
  display: none;
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0,0,0,0.4);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
  font-family: sans-serif;
}

#save-modal > div {
  background: #fff;
  padding: 20px 25px;
  border-radius: 12px;
  width: 320px;
  max-width: 90%;
  text-align: center;
  box-shadow: 0 8px 20px rgba(0,0,0,0.2);
  transition: transform 0.2s ease;
}

#save-modal p {
  margin-bottom: 12px;
  font-weight: 500;
  color: #333;
}

#filename-input {
  width: 90%;
  padding: 8px 10px;
  border: 1px solid #ccc;
  border-radius: 8px;
  margin-bottom: 15px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}

#filename-input:focus {
  border-color: #007bff;
}

#save-modal button {
  padding: 7px 15px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  margin: 0 5px;
  transition: background 0.2s, color 0.2s;
}

#save-confirm-btn {
  background: #007bff;
  color: #fff;
}

#save-confirm-btn:hover {
  background: #0056b3;
}

#save-cancel-btn {
  background: #f0f0f0;
  color: #333;
}

#save-cancel-btn:hover {
  background: #e0e0e0;
}

.bg-grid {
  background-color: #ffffff;
  background-image: url("data:image/svg+xml,%3Csvg width='20' height='20' viewBox='0 0 20 20' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%23dddddd' fill-opacity='0.6'%3E%3Crect width='1' height='20'/%3E%3Crect width='20' height='1' y='19'/%3E%3C/g%3E%3C/svg%3E");
  background-size: 20px 20px;
}

.bg-dots {
  background-color: #ffffff;
  background-image: url("data:image/svg+xml,%3Csvg width='20' height='20' viewBox='0 0 20 20' xmlns='http://www.w3.org/2000/svg'%3E%3Ccircle cx='10' cy='10' r='1.5' fill='%23aaaaaa' fill-opacity='0.7'/%3E%3C/svg%3E");
  background-size: 20px 20px;
  background-position: 10px 10px;
  background-repeat: repeat;
}

.bg-lines {
  background-color: #ffffff;
  background-image: url("data:image/svg+xml,%3Csvg width='1' height='40' viewBox='0 0 0 1 40' xmlns='http://www.w3.org/2000/svg'%3E%3Crect width='1' height='1' fill='%23dddddd'/%3E%3C/svg%3E");
  background-size: 100% 40px;
  background-repeat: repeat-y;
}

.bg-four-lines {
  background-color: #ffffff;
  background-image: url("data:image/svg+xml,%3Csvg width='1' height='40' viewBox='0 0 0 1 40' xmlns='http://www.w3.org/2000/svg'%3E%3Crect width='1' height='1' fill='%23dddddd'/%3E%3C/svg%3E");
  background-size: 100% 20px;
  background-repeat: repeat-y;
}

.bg-dashed {
  background-color: #ffffff;
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='10' viewBox='0 0 10 10' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M0,5 L10,5' stroke='%23dddddd' stroke-width='2' stroke-dasharray='1,8'/%3E%3C/svg%3E"),
                    url("data:image/svg+xml,%3Csvg width='10' height='10' viewBox='0 0 10 10' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M5,0 L5,10' stroke='%23dddddd' stroke-width='2' stroke-dasharray='1,8'/%3E%3C/svg%3E");
  background-size: 10px 10px;
  background-repeat: repeat;
}

.bg-checker {
  background-color: #ffffff;
  background-image: url("data:image/svg+xml,%3Csvg width='20' height='20' viewBox='0 0 20 20' xmlns='http://www.w3.org/2000/svg'%3E%3Crect width='10' height='10' fill='%23dddddd' fill-opacity='0.5'/%3E%3Crect x='10' y='10' width='10' height='10' fill='%23dddddd' fill-opacity='0.5'/%3E%3C/svg%3E");
  background-size: 20px 20px;
}

.gradient-blue {
  background: linear-gradient(135deg, #6B73FF 0%, #000DFF 100%);
}

.gradient-sunset {
  background: linear-gradient(135deg, #FF6B6B 0%, #FFA500 50%, #FFEE00 100%);
}

.gradient-ocean {
  background: linear-gradient(135deg, #00B4DB 0%, #0083B0 100%);
}

.gradient-green {
  background: linear-gradient(135deg, #56ab2f 0%, #a8e063 100%);
}

.gradient-purple {
  background: linear-gradient(135deg, #4776E6 0%, #8E54E9 100%);
}

.gradient-red {
  background: linear-gradient(135deg, #e53935 0%, #e35d5b 100%);
}

.gradient-rainbow {
  background: linear-gradient(135deg, #f00 0%, #ff7f00 20%, #ff0 40%, #0f0 60%, #00f 80%, #8b00ff 100%);
}

@media print {
  body * {
    visibility: hidden;
  }

  .flashcard-frame,
  .flashcard-frame * {
    visibility: visible;
  }

  .flashcard-frame {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    border: none;
    box-shadow: none;
    margin: 0;
    padding: 0;
  }

  .flashcard-frame img,
  .flashcard-frame canvas {
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;
  }

  .element img {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: contain;
  }

  .toolbox,
  .tab-content,
  .header {
    display: none !important;
  }
}